<!DOCTYPE html>
<html lang="zh-CN">
  <head>
  <meta charset="UTF-8">
  <meta 
    name="viewport"
    content="width=device-width, initial-scale=1.0, minimum-scale=1.0">
  <meta 
    http-equiv="X-UA-Compatible" 
    content="ie=edge">
  <meta 
    name="theme-color" 
    content="#fff" 
    id="theme-color">
  <meta 
    name="description" 
    content="hexoDemo用于介绍如何使用hexo">
  <link 
    rel="icon" 
    href="/">
  <title>03-CSS样式表和选择器</title>
  
    
      <meta 
        property="og:title" 
        content="03-CSS样式表和选择器">
    
    
      <meta 
        property="og:url" 
        content="https://zwl5670.gitee.io/2002/01/01/02-CSS%E5%9F%BA%E7%A1%80/03-CSS%E6%A0%B7%E5%BC%8F%E8%A1%A8%E5%92%8C%E9%80%89%E6%8B%A9%E5%99%A8/index.html">
    
    
      <meta 
        property="og:img" 
        content="https://img.songhn.com/img/Y67gdd.png">
    
    
      <meta 
        property="og:img" 
        content="hexoDemo用于介绍如何使用hexo">
    
    
      <meta 
        property="og:type" 
        content="article">
      <meta 
        property="og:article:published_time" 
        content="2002-01-01">
      <meta 
        property="og:article:modified_time" 
        content="2023-03-14">
      <meta 
        property="og:article:author" 
        content="zwl5670">
      
        
          <meta 
            property="og:article:tag" 
            content="CSS基础">
        
      
    
  
  
  <link rel="preload" href="//at.alicdn.com/t/font_1946621_i1kgafibvw.css" as="style" >
  <link rel="preload" href="//at.alicdn.com/t/font_1952792_89b4ac4k4up.css" as="style" >
  <link rel="preload" href="/css/main.css" as="style" >
  
  <link rel="modulepreload" href="//instant.page/5.1.0">
  
  <link rel="stylesheet" href="/css/main.css">
  
  <link rel="stylesheet" href="//at.alicdn.com/t/font_1946621_i1kgafibvw.css">
  
  <link rel="stylesheet" href="//at.alicdn.com/t/font_1952792_89b4ac4k4up.css">
  
  
  
    <link rel="stylesheet" href="/js/lib/lightbox/baguetteBox.min.css">
  
  <script>
    function loadScript(url, cb) {
      var script = document.createElement('script');
      script.src = url;
      if (cb) script.onload = cb;
      script.async = true;
      document.body.appendChild(script);
    }
    function loadCSS(href, data, attr) {
      var sheet = document.createElement('link');
      sheet.ref = 'stylesheet';
      sheet.href = href;
      sheet.dataset[data] = attr;
      document.head.appendChild(sheet);
    }
    function changeCSS(cssFile, data, attr) {
      var oldlink = document.querySelector(data);
      var newlink = document.createElement("link");
      newlink.setAttribute("rel", "stylesheet");
      newlink.setAttribute("href", cssFile);
      newlink.dataset.prism = attr;
      document.head.replaceChild(newlink, oldlink);
    }
  </script>
  
    
  
  <script>
    // control reverse button
    var reverseDarkList = {
      dark: 'light',
      light: 'dark'
    };
    var themeColor = {
      dark: '#1c1c1e',
      light: '#fff'
    }
    // get the data of css prefers-color-scheme
    var getCssMediaQuery = function() {
      return window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light';
    };
    // reverse current darkmode setting function
    var reverseDarkModeSetting = function() {
      var setting = localStorage.getItem('user-color-scheme');
      if(reverseDarkList[setting]) {
        setting = reverseDarkList[setting];
      } else if(setting === null) {
        setting = reverseDarkList[getCssMediaQuery()];
      } else {
        return;
      }
      localStorage.setItem('user-color-scheme', setting);
      return setting;
    };
    // apply current darkmode setting
  </script>
  
    <script>
      var setDarkmode = function(mode) {
      var setting = mode || localStorage.getItem('user-color-scheme');
      if(setting === getCssMediaQuery()) {
        document.documentElement.removeAttribute('data-user-color-scheme');
        localStorage.removeItem('user-color-scheme');
        document.getElementById('theme-color').content = themeColor[setting];
        document.getElementById('theme-color').dataset.mode = setting;
      } else if(reverseDarkList[setting]) {
        document.documentElement.setAttribute('data-user-color-scheme', setting);
        document.getElementById('theme-color').content = themeColor[setting];
        document.getElementById('theme-color').dataset.mode = setting;
      } else {
        document.documentElement.removeAttribute('data-user-color-scheme');
        localStorage.removeItem('user-color-scheme');
        document.getElementById('theme-color').content = themeColor[getCssMediaQuery()];
        document.getElementById('theme-color').dataset.mode = getCssMediaQuery();
      }
    };
    setDarkmode();
    </script>
  
  
  
    <link rel="preload" href="/js/lib/lightbox/baguetteBox.min.js" as="script">
    <link rel="preload" href="/js/lib/lightbox/baguetteBox.min.css" as="style" >
  
  
    <link rel="preload" href="/js/lib/lozad.min.js" as="script">
  
  
  
  
  
  
<meta name="generator" content="Hexo 6.3.0"></head>

  <body>
    <div class="wrapper">
       
      <nav class="navbar">
  <div class="navbar-logo">
    <a class="navbar-logo-main" href="/">
      
      <span class="navbar-logo-dsc">前端面试</span>
      </a>
  </div>
  <div class="navbar-menu">
    
      <a 
        href="/" 
        class="navbar-menu-item">
        
          首页
        
      </a>
    
      <a 
        href="/archives" 
        class="navbar-menu-item">
        
          归档
        
      </a>
    
      <a 
        href="/tags" 
        class="navbar-menu-item">
        
          标签
        
      </a>
    
      <a 
        href="/categories" 
        class="navbar-menu-item">
        
          分类
        
      </a>
    
      <a 
        href="/about" 
        class="navbar-menu-item">
        
          关于
        
      </a>
    
      <a 
        href="/links" 
        class="navbar-menu-item">
        
          友链
        
      </a>
    
    <button 
      class="navbar-menu-item darknavbar navbar-menu-btn" 
      aria-label="Toggle dark mode"
      id="dark">
      <i class="iconfont icon-weather"></i>
    </button>
    <button 
      class="navbar-menu-item searchnavbar navbar-menu-btn" 
      aria-label="Toggle search"
      id="search">
      <!-- <i 
        class="iconfont icon-search" 
        style="font-size: 1.2rem; font-weight: 400;">
      </i> -->
      <svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img"
        class="iconify iconify--ion" width="28" height="28" preserveAspectRatio="xMidYMid meet" viewBox="0 0 512 512">
        <path fill="none" stroke="currentColor" stroke-miterlimit="10" stroke-width="28"
          d="M256 80a176 176 0 1 0 176 176A176 176 0 0 0 256 80Z"></path>
        <path fill="none" stroke="currentColor" stroke-miterlimit="10" stroke-width="28"
          d="M232 160a72 72 0 1 0 72 72a72 72 0 0 0-72-72Z"></path>
        <path fill="none" stroke="currentColor" stroke-linecap="round" stroke-miterlimit="10" stroke-width="28"
          d="M283.64 283.64L336 336"></path>
      </svg>
    </button>
  </div>
</nav> 
      
      <div 
        id="local-search" 
        style="display: none">
        <input
          class="navbar-menu-item"
          id="search-input"
          placeholder="请输入搜索内容..." />
        <div id="search-content"></div>
      </div>
      
      <div class="section-wrap">
        <div class="container">
          <div class="columns">
            <aside class="left-column">
              
              <div class="card card-author">
                
  <img 
    src="https://img.songhn.com/img/Y67gdd.png" 
    class="author-img"
    width="88"
    height="88"
    alt="author avatar">

<p class="author-name">zwl5670</p>
<p class="author-description">前端学习资料</p>
<div class="author-message">
  <a 
    class="author-posts-count" 
    href="/archives">
    <span>215</span>
    <span>文章</span>
  </a>
  <a 
    class="author-categories-count" 
    href="/categories">
    <span>14</span>
    <span>分类</span>
  </a>
  <a 
    class="author-tags-count" 
    href="/tags">
    <span>14</span>
    <span>标签</span>
  </a>
</div>

              </div>
               <div class="sticky-tablet">
  
  
    <article class="display-when-two-columns spacer">
      <div class="card card-content toc-card">
        <div class="toc-header">
  <i 
    class="iconfont icon-menu" 
    style="padding-right: 2px;">
  </i>目录
</div>
<ol class="toc"><li class="toc-item toc-level-2"><a class="toc-link" href="#%E6%9C%AC%E6%96%87%E4%B8%BB%E8%A6%81%E5%86%85%E5%AE%B9"><span class="toc-text">本文主要内容</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%89%8D%E8%A8%80"><span class="toc-text">前言</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#CSS-%E6%A6%82%E8%BF%B0"><span class="toc-text">CSS 概述</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#CSS-%E7%9A%84%E9%87%8D%E7%82%B9%E7%9F%A5%E8%AF%86%E7%82%B9"><span class="toc-text">CSS 的重点知识点</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#CSS-%E6%95%B4%E4%BD%93%E6%84%9F%E7%9F%A5"><span class="toc-text">CSS 整体感知</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#CSS-%E8%AF%AD%E6%B3%95"><span class="toc-text">CSS 语法</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#css-%E4%BB%A3%E7%A0%81%E7%9A%84%E6%B3%A8%E9%87%8A"><span class="toc-text">css 代码的注释</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#CSS-%E7%9A%84%E4%B8%80%E4%BA%9B%E7%AE%80%E5%8D%95%E5%B8%B8%E8%A7%81%E7%9A%84%E5%B1%9E%E6%80%A7"><span class="toc-text">CSS 的一些简单常见的属性</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#CSS-%E7%9A%84%E4%B9%A6%E5%86%99%E6%96%B9%E5%BC%8F"><span class="toc-text">CSS 的书写方式</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#1%E3%80%81CSS-%E5%92%8C-HTML-%E7%BB%93%E5%90%88%E6%96%B9%E5%BC%8F%E4%B8%80%EF%BC%9A%E8%A1%8C%E5%86%85%E6%A0%B7%E5%BC%8F"><span class="toc-text">1、CSS 和 HTML 结合方式一：行内样式</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#2%E3%80%81CSS-%E5%92%8C-HTML-%E7%BB%93%E5%90%88%E6%96%B9%E5%BC%8F%E4%BA%8C%EF%BC%9A%E5%86%85%E5%B5%8C%E6%A0%B7%E5%BC%8F%E8%A1%A8"><span class="toc-text">2、CSS 和 HTML 结合方式二：内嵌样式表</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#3%E3%80%81CSS-%E5%92%8C-HTML-%E7%BB%93%E5%90%88%E6%96%B9%E5%BC%8F%E4%B8%89%EF%BC%9A%E5%BC%95%E5%85%A5%E5%A4%96%E9%83%A8%E6%A0%B7%E5%BC%8F%E8%A1%A8-css-%E6%96%87%E4%BB%B6"><span class="toc-text">3、CSS 和 HTML 结合方式三：引入外部样式表 css 文件</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#CSS-%E7%9A%84%E5%9B%9B%E7%A7%8D%E5%9F%BA%E6%9C%AC%E9%80%89%E6%8B%A9%E5%99%A8"><span class="toc-text">CSS 的四种基本选择器</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#1%E3%80%81%E6%A0%87%E7%AD%BE%E9%80%89%E6%8B%A9%E5%99%A8%EF%BC%9A%E9%80%89%E6%8B%A9%E5%99%A8%E7%9A%84%E5%90%8D%E5%AD%97%E4%BB%A3%E8%A1%A8-html-%E9%A1%B5%E9%9D%A2%E4%B8%8A%E7%9A%84%E6%A0%87%E7%AD%BE"><span class="toc-text">1、标签选择器：选择器的名字代表 html 页面上的标签</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#2%E3%80%81ID-%E9%80%89%E6%8B%A9%E5%99%A8%EF%BC%9A%E8%A7%84%E5%AE%9A%E7%94%A8-%E6%9D%A5%E5%AE%9A%E4%B9%89"><span class="toc-text">2、ID 选择器：规定用#来定义</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#3%E3%80%81%E7%B1%BB%E9%80%89%E6%8B%A9%E5%99%A8%EF%BC%9A%E8%A7%84%E5%AE%9A%E7%94%A8%E5%9C%86%E7%82%B9-%E6%9D%A5%E5%AE%9A%E4%B9%89"><span class="toc-text">3、类选择器：规定用圆点.来定义</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#4%E3%80%81%E9%80%9A%E9%85%8D%E7%AC%A6-%EF%BC%9A%E5%8C%B9%E9%85%8D%E4%BB%BB%E4%BD%95%E6%A0%87%E7%AD%BE"><span class="toc-text">4、通配符*：匹配任何标签</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#CSS-%E7%9A%84%E5%87%A0%E7%A7%8D%E9%AB%98%E7%BA%A7%E9%80%89%E6%8B%A9%E5%99%A8"><span class="toc-text">CSS 的几种高级选择器</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#1%E3%80%81%E5%90%8E%E4%BB%A3%E9%80%89%E6%8B%A9%E5%99%A8-%E5%AE%9A%E4%B9%89%E7%9A%84%E6%97%B6%E5%80%99%E7%94%A8%E7%A9%BA%E6%A0%BC%E9%9A%94%E5%BC%80"><span class="toc-text">1、后代选择器: 定义的时候用空格隔开</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#2%E3%80%81%E4%BA%A4%E9%9B%86%E9%80%89%E6%8B%A9%E5%99%A8%EF%BC%9A%E5%AE%9A%E4%B9%89%E7%9A%84%E6%97%B6%E5%80%99%E7%B4%A7%E5%AF%86%E7%9B%B8%E8%BF%9E"><span class="toc-text">2、交集选择器：定义的时候紧密相连</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#3%E3%80%81%E5%B9%B6%E9%9B%86%E9%80%89%E6%8B%A9%E5%99%A8%EF%BC%9A%E5%AE%9A%E4%B9%89%E7%9A%84%E6%97%B6%E5%80%99%E7%94%A8%E9%80%97%E5%8F%B7%E9%9A%94%E5%BC%80"><span class="toc-text">3、并集选择器：定义的时候用逗号隔开</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E4%B8%80%E4%BA%9B-CSS3-%E9%80%89%E6%8B%A9%E5%99%A8"><span class="toc-text">一些 CSS3 选择器</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%B5%8F%E8%A7%88%E5%99%A8%E7%9A%84%E5%85%BC%E5%AE%B9%E6%80%A7%E9%97%AE%E9%A2%98"><span class="toc-text">浏览器的兼容性问题</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#1-%E5%AD%90%E4%BB%A3%E9%80%89%E6%8B%A9%E5%99%A8%EF%BC%8C%E7%94%A8%E7%AC%A6%E5%8F%B7-gt-%E8%A1%A8%E7%A4%BA"><span class="toc-text">1.子代选择器，用符号&gt;表示</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#2-%E5%BA%8F%E9%80%89%E6%8B%A9%E5%99%A8"><span class="toc-text">2.序选择器</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#3-%E4%B8%8B%E4%B8%80%E4%B8%AA%E5%85%84%E5%BC%9F%E9%80%89%E6%8B%A9%E5%99%A8"><span class="toc-text">3.下一个兄弟选择器</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E6%88%91%E7%9A%84%E5%85%AC%E4%BC%97%E5%8F%B7"><span class="toc-text">我的公众号</span></a></li></ol>
      </div>
    </article>
  
  
  <article class="card card-content categories-widget">
    <div class="categories-card">
  <div class="categories-header">
    <i 
      class="iconfont icon-fenlei" 
      style="padding-right: 2px;">
    </i>分类
  </div>
  <div class="categories-list">
    
      <a href="/categories/%E5%89%8D%E7%AB%AF%E5%B7%A5%E5%85%B7/">
        <div class="categories-list-item">
          前端工具
          <span class="categories-list-item-badge">13</span>
        </div>
      </a>
    
      <a href="/categories/CSS%E5%9F%BA%E7%A1%80/">
        <div class="categories-list-item">
          CSS基础
          <span class="categories-list-item-badge">18</span>
        </div>
      </a>
    
      <a href="/categories/CSS%E8%BF%9B%E9%98%B6/">
        <div class="categories-list-item">
          CSS进阶
          <span class="categories-list-item-badge">9</span>
        </div>
      </a>
    
      <a href="/categories/HTML/">
        <div class="categories-list-item">
          HTML
          <span class="categories-list-item-badge">12</span>
        </div>
      </a>
    
      <a href="/categories/JS%E5%9F%BA%E7%A1%80/">
        <div class="categories-list-item">
          JS基础
          <span class="categories-list-item-badge">56</span>
        </div>
      </a>
    
      <a href="/categories/JS%E5%9F%BA%E7%A1%80ES6/">
        <div class="categories-list-item">
          JS基础ES6
          <span class="categories-list-item-badge">10</span>
        </div>
      </a>
    
      <a href="/categories/JS%E5%9F%BA%E7%A1%80%E5%BC%82%E6%AD%A5/">
        <div class="categories-list-item">
          JS基础异步
          <span class="categories-list-item-badge">11</span>
        </div>
      </a>
    
      <a href="/categories/JS%E8%BF%9B%E9%98%B6/">
        <div class="categories-list-item">
          JS进阶
          <span class="categories-list-item-badge">11</span>
        </div>
      </a>
    
      <a href="/categories/%E7%A7%BB%E5%8A%A8web/">
        <div class="categories-list-item">
          移动web
          <span class="categories-list-item-badge">3</span>
        </div>
      </a>
    
      <a href="/categories/CSS%E5%92%8CDOM/">
        <div class="categories-list-item">
          CSS和DOM
          <span class="categories-list-item-badge">6</span>
        </div>
      </a>
    
      <a href="/categories/Node/">
        <div class="categories-list-item">
          Node
          <span class="categories-list-item-badge">16</span>
        </div>
      </a>
    
      <a href="/categories/Vue%E5%9F%BA%E7%A1%80/">
        <div class="categories-list-item">
          Vue基础
          <span class="categories-list-item-badge">15</span>
        </div>
      </a>
    
      <a href="/categories/%E6%80%A7%E8%83%BD%E4%BC%98%E5%8C%96/">
        <div class="categories-list-item">
          性能优化
          <span class="categories-list-item-badge">7</span>
        </div>
      </a>
    
      <a href="/categories/%E5%89%8D%E7%AB%AF%E5%B7%A5%E7%A8%8B%E5%8C%96/">
        <div class="categories-list-item">
          前端工程化
          <span class="categories-list-item-badge">8</span>
        </div>
      </a>
    
  </div>
</div>
  </article>
  
  <article class="card card-content tags-widget">
    <div class="tags-card">
  <div class="tags-header">
    <i 
      class="iconfont icon-biaoqian" 
      style="padding-right: 2px;">
    </i>热门标签
  </div>
  <div class="tags-list">
    
      <a 
        href="/tags/JS%E5%9F%BA%E7%A1%80/" 
        title="JS基础">
        <div class="tags-list-item">JS基础</div>
      </a>
    
      <a 
        href="/tags/CSS%E5%9F%BA%E7%A1%80/" 
        title="CSS基础">
        <div class="tags-list-item">CSS基础</div>
      </a>
    
      <a 
        href="/tags/Node/" 
        title="Node">
        <div class="tags-list-item">Node</div>
      </a>
    
      <a 
        href="/tags/Vue%E5%9F%BA%E7%A1%80/" 
        title="Vue基础">
        <div class="tags-list-item">Vue基础</div>
      </a>
    
      <a 
        href="/tags/%E5%89%8D%E7%AB%AF%E5%B7%A5%E5%85%B7/" 
        title="前端工具">
        <div class="tags-list-item">前端工具</div>
      </a>
    
      <a 
        href="/tags/HTML/" 
        title="HTML">
        <div class="tags-list-item">HTML</div>
      </a>
    
      <a 
        href="/tags/JS%E8%BF%9B%E9%98%B6/" 
        title="JS进阶">
        <div class="tags-list-item">JS进阶</div>
      </a>
    
      <a 
        href="/tags/JS%E5%9F%BA%E7%A1%80%E5%BC%82%E6%AD%A5/" 
        title="JS基础异步">
        <div class="tags-list-item">JS基础异步</div>
      </a>
    
      <a 
        href="/tags/JS%E5%9F%BA%E7%A1%80ES6/" 
        title="JS基础ES6">
        <div class="tags-list-item">JS基础ES6</div>
      </a>
    
      <a 
        href="/tags/CSS%E8%BF%9B%E9%98%B6/" 
        title="CSS进阶">
        <div class="tags-list-item">CSS进阶</div>
      </a>
    
      <a 
        href="/tags/%E5%89%8D%E7%AB%AF%E5%B7%A5%E7%A8%8B%E5%8C%96/" 
        title="前端工程化">
        <div class="tags-list-item">前端工程化</div>
      </a>
    
      <a 
        href="/tags/%E6%80%A7%E8%83%BD%E4%BC%98%E5%8C%96/" 
        title="性能优化">
        <div class="tags-list-item">性能优化</div>
      </a>
    
      <a 
        href="/tags/CSS%E5%92%8CDOM/" 
        title="CSS和DOM">
        <div class="tags-list-item">CSS和DOM</div>
      </a>
    
      <a 
        href="/tags/%E7%A7%BB%E5%8A%A8web/" 
        title="移动web">
        <div class="tags-list-item">移动web</div>
      </a>
    
  </div>
</div>
  </article>
  
  
</div>
            </aside>
            <main class="main-column">
              
<article class="card card-content">
  <header>
    <h1 class="post-title">
      03-CSS样式表和选择器
    </h1>
  </header>
  <div class="post-meta post-show-meta">
    <time datetime="2001-12-31T16:00:00.000Z">
      <i 
        class="iconfont icon-calendar" 
        style="margin-right: 2px;">
      </i>
      <span>2002-01-01</span>
    </time>
    
      <span class="dot"></span>
      
        <a 
          href="/categories/CSS%E5%9F%BA%E7%A1%80/" 
          class="post-meta-link">
          CSS基础
        </a>
      
    
    
      <span class="dot"></span>
      <span>6k 字</span>
    
  </div>
  
    <div 
      class="post-meta post-show-meta" 
      style="margin-top: -10px;">
      <div style="display: flex; align-items: center;">
        <i 
          class="iconfont icon-biaoqian" 
          style="margin-right: 2px; font-size: 1.15rem;">
        </i>
        
          
          <a 
            href="/tags/CSS%E5%9F%BA%E7%A1%80/" 
            class="post-meta-link">
            CSS基础
          </a>
        
      </div>
    </div>
  
  </header>
  <div 
    id="section" 
    class="post-content">
    <p><ArticleTopAd></ArticleTopAd></p>
<h2 id="本文主要内容"><a href="#本文主要内容" class="headerlink" title="本文主要内容"></a>本文主要内容</h2><ul>
<li>CSS 概述</li>
<li>CSS 和 HTML 结合的三种方式：<code>行内样式表</code>、<code>内嵌样式表</code>、<code>外部样式表</code></li>
<li>CSS 四种基本选择器：<code>标签选择器</code>、<code>类选择器</code>、<code>ID选择器</code>、<code>通用选择器</code></li>
<li>CSS 几种扩展选择器：<code>后代选择器</code>、<code>交集选择器</code>、<code>并集选择器</code></li>
<li>CSS 样式优先级</li>
</ul>
<h2 id="前言"><a href="#前言" class="headerlink" title="前言"></a>前言</h2><h2 id="CSS-概述"><a href="#CSS-概述" class="headerlink" title="CSS 概述"></a>CSS 概述</h2><p>CSS：Cascading Style Sheet，层叠样式表。CSS 的作用就是给 HTML 页面标签添加各种样式，<strong>定义网页的显示效果</strong>。简单一句话：CSS 将网页<strong>内容和显示样式进行分离</strong>，提高了显示功能。</p>
<p>css 的最新版本是 css3，<strong>我们目前学习的是 css2.1</strong>。 因为 css3 和 css2.1 不矛盾，必须先学 2.1 然后学 3。</p>
<p>接下来我们要讲一下为什么要使用 CSS。</p>
<p><strong>HTML 的缺陷：</strong></p>
<ol>
<li>不能够适应多种设备</li>
<li>要求浏览器必须智能化足够庞大</li>
<li>数据和显示没有分开</li>
<li>功能不够强大</li>
</ol>
<p><strong>CSS 优点：</strong></p>
<ol>
<li>使数据和显示分开</li>
<li>降低网络流量</li>
<li>使整个网站视觉效果一致</li>
<li>使开发效率提高了（耦合性降低，一个人负责写 html，一个人负责写 css）</li>
</ol>
<p>比如说，有一个样式需要在一百个页面上显示，如果是 html 来实现，那要写一百遍，现在有了 css，只要写一遍。现在，html 只提供数据和一些控件，完全交给 css 提供各种各样的样式。</p>
<h3 id="CSS-的重点知识点"><a href="#CSS-的重点知识点" class="headerlink" title="CSS 的重点知识点"></a>CSS 的重点知识点</h3><p>盒子模型、浮动、定位</p>
<h3 id="CSS-整体感知"><a href="#CSS-整体感知" class="headerlink" title="CSS 整体感知"></a>CSS 整体感知</h3><p>我们先来看一段简单的 css 代码：</p>
<pre class="highlight"><span class="line"><span class="meta">&lt;!DOCTYPE <span class="keyword">html</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">html</span> <span class="attr">lang</span>=<span class="string">&quot;en&quot;</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">head</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">meta</span> <span class="attr">charset</span>=<span class="string">&quot;UTF-8&quot;</span> /&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">title</span>&gt;</span>Document<span class="tag">&lt;/<span class="name">title</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">style</span>&gt;</span><span class="language-css"></span></span><br><span class="line"><span class="language-css">            <span class="selector-tag">p</span> &#123;</span></span><br><span class="line"><span class="language-css">                <span class="attribute">color</span>: red;</span></span><br><span class="line"><span class="language-css">                <span class="attribute">font-size</span>: <span class="number">30px</span>;</span></span><br><span class="line"><span class="language-css">                <span class="attribute">text-decoration</span>: underline;</span></span><br><span class="line"><span class="language-css">                <span class="attribute">font-weight</span>: bold;</span></span><br><span class="line"><span class="language-css">                <span class="attribute">text-align</span>: center;</span></span><br><span class="line"><span class="language-css">                <span class="attribute">font-style</span>: italic;</span></span><br><span class="line"><span class="language-css">            &#125;</span></span><br><span class="line"><span class="language-css">            <span class="selector-tag">h1</span> &#123;</span></span><br><span class="line"><span class="language-css">                <span class="attribute">color</span>: blue;</span></span><br><span class="line"><span class="language-css">                <span class="attribute">font-size</span>: <span class="number">50px</span>;</span></span><br><span class="line"><span class="language-css">                <span class="attribute">font-weight</span>: bold;</span></span><br><span class="line"><span class="language-css">                <span class="attribute">background-color</span>: pink;</span></span><br><span class="line"><span class="language-css">            &#125;</span></span><br><span class="line"><span class="language-css">        </span><span class="tag">&lt;/<span class="name">style</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">head</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">body</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">h1</span>&gt;</span>我是大标题<span class="tag">&lt;/<span class="name">h1</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">p</span>&gt;</span>我是内容<span class="tag">&lt;/<span class="name">p</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">body</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">html</span>&gt;</span></span><br></pre>

<p>解释如下：</p>
<p><img  srcset="data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20300%20300'%3E%3C/svg%3E" data-src="http://img.smyhvae.com/20170710_1605.png" class="lozad post-image"src="http://img.smyhvae.com/20170710_1605.png"></p>
<p>我们写 css 的地方是 style 标签，就是“样式”的意思，写在 head 里面。后面的课程中我们将知道，css 也可以写在单独的文件里面，现在我们先写在 style 标签里面。</p>
<p>如果在 sublime 中输入<code>&lt;st</code>或者<code>&lt;style</code>然后按 tab 键，可以自动生成的格式如下：（建议）</p>
<pre class="highlight"><span class="line"><span class="tag">&lt;<span class="name">style</span> <span class="attr">type</span>=<span class="string">&quot;text/css&quot;</span>&gt;</span><span class="tag">&lt;/<span class="name">style</span>&gt;</span></span><br></pre>

<p>type 表示“类型”，text 就是“纯文本”，css 也是纯文本。</p>
<p>但是，如果在 sublime 中输入<code>st</code>或者<code>style</code>然后按 tab 键，可以自动生成的格式如下：（不建议）</p>
<pre class="highlight"><span class="line"><span class="tag">&lt;<span class="name">style</span>&gt;</span><span class="tag">&lt;/<span class="name">style</span>&gt;</span></span><br></pre>

<p>css 对换行不敏感，对空格也不敏感。但是一定要有标准的语法。冒号，分号都不能省略。</p>
<h2 id="CSS-语法"><a href="#CSS-语法" class="headerlink" title="CSS 语法"></a>CSS 语法</h2><p><strong>语法格式：</strong>（其实就是键值对）</p>
<pre class="highlight"><span class="line">选择器&#123; 属性名: 属性值; 属性名: 属性值; &#125;</span><br></pre>

<p>或者可以写成：</p>
<pre class="highlight"><span class="line">选择器 &#123;</span><br><span class="line">    k: v;</span><br><span class="line">    k: v;</span><br><span class="line">    k: v;</span><br><span class="line">    k: v;</span><br><span class="line">&#125;</span><br><span class="line">选择器 &#123;</span><br><span class="line">    k: v;</span><br><span class="line">    k: v;</span><br><span class="line">    k: v;</span><br><span class="line">    k: v;</span><br><span class="line">&#125;</span><br></pre>

<p><strong>解释：</strong></p>
<ul>
<li>选择器代表页面上的某类元素，选择器后一定是大括号。</li>
<li>属性名后必须用冒号隔开，属性值后用分号（最后一个属性可以不用分号，但最好还是加上分号）。</li>
<li>冒号和属性值之间可以留一个空格（编程习惯的经验）。</li>
<li>如果一个属性有多个值的话，那么多个值用<strong>空格</strong>隔开。</li>
</ul>
<p><strong>举例：</strong></p>
<pre class="highlight"><span class="line"><span class="selector-tag">p</span> &#123;</span><br><span class="line">    <span class="attribute">color</span>: red;</span><br><span class="line">&#125;</span><br></pre>

<p><strong>完整版代码举例：</strong></p>
<pre class="highlight"><span class="line"><span class="tag">&lt;<span class="name">style</span> <span class="attr">type</span>=<span class="string">&quot;text/css&quot;</span>&gt;</span><span class="language-css"></span></span><br><span class="line"><span class="language-css">    <span class="selector-tag">p</span> &#123;</span></span><br><span class="line"><span class="language-css">        <span class="attribute">font-weight</span>: bold;</span></span><br><span class="line"><span class="language-css">        <span class="attribute">font-style</span>: italic;</span></span><br><span class="line"><span class="language-css">        <span class="attribute">color</span>: red;</span></span><br><span class="line"><span class="language-css">    &#125;</span></span><br><span class="line"><span class="language-css"></span><span class="tag">&lt;/<span class="name">style</span>&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="tag">&lt;<span class="name">body</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">p</span>&gt;</span>洗白白<span class="tag">&lt;/<span class="name">p</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">p</span>&gt;</span>你懂得<span class="tag">&lt;/<span class="name">p</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">p</span>&gt;</span>我不会就这样轻易的狗带<span class="tag">&lt;/<span class="name">p</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">body</span>&gt;</span></span><br></pre>

<p>效果：</p>
<p><img  srcset="data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20300%20300'%3E%3C/svg%3E" data-src="http://img.smyhvae.com/2015-10-03-css-01.png" class="lozad post-image"src="http://img.smyhvae.com/2015-10-03-css-01.png"></p>
<h3 id="css-代码的注释"><a href="#css-代码的注释" class="headerlink" title="css 代码的注释"></a>css 代码的注释</h3><p><strong>格式：</strong></p>
<pre class="highlight"><span class="line"><span class="tag">&lt;<span class="name">style</span> <span class="attr">type</span>=<span class="string">&quot;text/css&quot;</span>&gt;</span><span class="language-css"></span></span><br><span class="line"><span class="language-css">    <span class="comment">/*</span></span></span><br><span class="line"><span class="comment"><span class="language-css">		具体的注释</span></span></span><br><span class="line"><span class="comment"><span class="language-css">	*/</span></span></span><br><span class="line"><span class="language-css"></span></span><br><span class="line"><span class="language-css">    <span class="selector-tag">p</span> &#123;</span></span><br><span class="line"><span class="language-css">        <span class="attribute">font-weight</span>: bold;</span></span><br><span class="line"><span class="language-css">        <span class="attribute">font-style</span>: italic;</span></span><br><span class="line"><span class="language-css">        <span class="attribute">color</span>: red;</span></span><br><span class="line"><span class="language-css">    &#125;</span></span><br><span class="line"><span class="language-css"></span><span class="tag">&lt;/<span class="name">style</span>&gt;</span></span><br></pre>

<p>注意：CSS 只有<code>/* */</code>这种注释，没有<code>//</code>这种注释。而且注释要写在<code>&lt;style&gt;</code>标签里面才算生效哦。</p>
<p>接下来，我们要开始真正地讲 css 的知识咯。</p>
<p>css 怎么学？CSS 有两个知识部分：<br>1） 选择器，怎么选；<br>2） 属性，样式是什么</p>
<h2 id="CSS-的一些简单常见的属性"><a href="#CSS-的一些简单常见的属性" class="headerlink" title="CSS 的一些简单常见的属性"></a>CSS 的一些简单常见的属性</h2><blockquote>
<p>我们先来接触 CSS 的一些简单常见的属性，因为接下来需要用到。后期会专门用一篇文章来写 CSS 的属性。</p>
</blockquote>
<p>以下属性值中，括号中的内容表示 sublime 中的快捷键。</p>
<p><strong>字体颜色：</strong>（c）</p>
<pre class="highlight"><span class="line">color:red;</span><br></pre>

<p>color 属性的值，可以是英语单词，比如 red、blue、yellow 等等；也可以是 rgb、十六进制(后期详细讲)。</p>
<p><strong>字号大小：</strong>（fos）</p>
<pre class="highlight"><span class="line">font-size:40px;</span><br></pre>

<p>font 就是“字体”，size 就是“尺寸”。px 是“像素”。单位必须加，不加不行。</p>
<p><strong>背景颜色：</strong>（bgc）</p>
<pre class="highlight"><span class="line">background-color: blue;</span><br></pre>

<p>background 就是“背景”。</p>
<p><strong>加粗：</strong>（fwb）</p>
<pre class="highlight"><span class="line">font-weight: bold;</span><br></pre>

<p>font 是“字体” weight 是“重量”的意思，bold 粗。</p>
<p><strong>不加粗：</strong>（fwn）</p>
<pre class="highlight"><span class="line">font-weight: normal;</span><br></pre>

<p>normal 就是正常的意思。</p>
<p><strong>斜体：</strong>（fsi）</p>
<pre class="highlight"><span class="line">font-style: italic;</span><br></pre>

<p>italic 就是“斜体”。</p>
<p><strong>不斜体：</strong>（fsn）</p>
<pre class="highlight"><span class="line">font-style: normal;</span><br></pre>

<p><strong>下划线：</strong>（tdu）</p>
<pre class="highlight"><span class="line">text-decoration: underline;</span><br></pre>

<p>decoration 就是“装饰”的意思。</p>
<p><strong>没有下划线：</strong>（tdn）</p>
<pre class="highlight"><span class="line">text-decoration:none;</span><br></pre>

<h2 id="CSS-的书写方式"><a href="#CSS-的书写方式" class="headerlink" title="CSS 的书写方式"></a>CSS 的书写方式</h2><p>CSS 的书写方式，实就是问你 CSS 的代码放在哪个位置。CSS 代码理论上的位置是任意的，<strong>但通常写在<code>&lt;style&gt;</code>标签里</strong>。</p>
<p>CSS 的书写方式有三种：</p>
<ol>
<li><p><strong>行内样式</strong>：在某个特定的标签里采用 style <strong>属性</strong>。范围只针对此标签。</p>
</li>
<li><p><strong>内嵌样式</strong>（内联样式）：在页面的 head 标签里里采用<code>&lt;style&gt;</code><strong>标签</strong>。范围针对此页面。</p>
</li>
<li><p><strong>外链样式</strong>：引入外部样式表 CSS <strong>文件</strong>。这种引入方式又分为两种：</p>
<ul>
<li>3.1 采用<code>&lt;link&gt;</code>标签。例如：<code>&lt;link rel = &quot;stylesheet&quot; type = &quot;text/css&quot; href = &quot;a.css&quot;&gt;&lt;/link&gt;</code></li>
<li>3.2 采用 import 导入，必须写在<code>&lt;style&gt;</code>标签中。然后用类似于<code>@import url(a.css) ;</code>这种方式导入。</li>
</ul>
</li>
</ol>
<p>下面来详细讲一讲这三种方式。</p>
<h3 id="1、CSS-和-HTML-结合方式一：行内样式"><a href="#1、CSS-和-HTML-结合方式一：行内样式" class="headerlink" title="1、CSS 和 HTML 结合方式一：行内样式"></a>1、CSS 和 HTML 结合方式一：行内样式</h3><p>采用 style 属性。范围只针对此标签适用。</p>
<p>该方式比较灵活，但是对于多个相同标签的同一样式定义比较麻烦，适合局部修改。</p>
<p>举例：</p>
<pre class="highlight"><span class="line"><span class="tag">&lt;<span class="name">p</span> <span class="attr">style</span>=<span class="string">&quot;color:white;background-color:red&quot;</span>&gt;</span>我不会就这样轻易的狗带<span class="tag">&lt;/<span class="name">p</span>&gt;</span></span><br></pre>

<p>效果：</p>
<p><img  srcset="data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20300%20300'%3E%3C/svg%3E" data-src="http://img.smyhvae.com/2015-10-03-css-02.png" class="lozad post-image"src="http://img.smyhvae.com/2015-10-03-css-02.png"></p>
<h3 id="2、CSS-和-HTML-结合方式二：内嵌样式表"><a href="#2、CSS-和-HTML-结合方式二：内嵌样式表" class="headerlink" title="2、CSS 和 HTML 结合方式二：内嵌样式表"></a>2、CSS 和 HTML 结合方式二：内嵌样式表</h3><p>在 head 标签中加入<code>&lt;style&gt;</code>标签，对多个标签进行统一修改，范围针对此页面。</p>
<p>该方式可以对单个页面的样式进行统一设置，但对于局部不够灵活。</p>
<p>举例：</p>
<pre class="highlight"><span class="line"><span class="tag">&lt;<span class="name">style</span> <span class="attr">type</span>=<span class="string">&quot;text/css&quot;</span>&gt;</span><span class="language-css"></span></span><br><span class="line"><span class="language-css">    <span class="selector-tag">p</span> &#123;</span></span><br><span class="line"><span class="language-css">        <span class="attribute">font-weight</span>: bold;</span></span><br><span class="line"><span class="language-css">        <span class="attribute">font-style</span>: italic;</span></span><br><span class="line"><span class="language-css">        <span class="attribute">color</span>: red;</span></span><br><span class="line"><span class="language-css">    &#125;</span></span><br><span class="line"><span class="language-css"></span><span class="tag">&lt;/<span class="name">style</span>&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="tag">&lt;<span class="name">body</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">p</span>&gt;</span>洗白白<span class="tag">&lt;/<span class="name">p</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">p</span> <span class="attr">style</span>=<span class="string">&quot;color:blue&quot;</span>&gt;</span>你懂得<span class="tag">&lt;/<span class="name">p</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">body</span>&gt;</span></span><br></pre>

<p><img  srcset="data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20300%20300'%3E%3C/svg%3E" data-src="http://img.smyhvae.com/2015-10-03-css-03.png" class="lozad post-image"src="http://img.smyhvae.com/2015-10-03-css-03.png"></p>
<h3 id="3、CSS-和-HTML-结合方式三：引入外部样式表-css-文件"><a href="#3、CSS-和-HTML-结合方式三：引入外部样式表-css-文件" class="headerlink" title="3、CSS 和 HTML 结合方式三：引入外部样式表 css 文件"></a>3、CSS 和 HTML 结合方式三：引入外部样式表 css 文件</h3><p><strong>引入样式表文件</strong>的方式又分为两种：</p>
<ul>
<li><p>（1）<strong>采用<code>&lt;link&gt;</code>标签</strong>。例如：<code>&lt;link rel = &quot;stylesheet&quot; type = &quot;text/css&quot; href = &quot;a.css&quot;&gt;&lt;/link&gt;</code></p>
</li>
<li><p>（2）<strong>采用 import</strong>，必须写在<code>&lt;style&gt;</code>标签中，并且必须是第一句。例如：<code>@import url(a.css) ;</code></p>
</li>
</ul>
<blockquote>
<p>两种引入样式方式的区别：外部样式表中不能写<link>标签，但是可以写 import 语句。</p>
</blockquote>
<p><strong>具体操作如下：</strong></p>
<p>我们先在 html 页面的同级目录下新建一个<code>a.css</code>文件，那说明这里面的代码全是 css 代码，此时就没有必要再写<code>&lt;style&gt;</code>标签这几个字了。<br><code>a.css</code>的代码如下：</p>
<pre class="highlight"><span class="line"><span class="selector-tag">p</span> &#123;</span><br><span class="line">    <span class="attribute">border</span>: <span class="number">1px</span> solid red;</span><br><span class="line">    <span class="attribute">font-size</span>: <span class="number">40px</span>;</span><br><span class="line">&#125;</span><br></pre>

<p>上方的 css 代码中，注意像素要带上 px 这个单位，不然不生效。<br>然后我们在 html 文件中通过<code>&lt;link&gt;</code>标签引入这个 css 文件就行了。效果如下：</p>
<p><img  srcset="data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20300%20300'%3E%3C/svg%3E" data-src="http://img.smyhvae.com/2015-10-03-css-04.png" class="lozad post-image"src="http://img.smyhvae.com/2015-10-03-css-04.png"></p>
<p>这里再讲一个补充的知识：**<code>&lt;link&gt;</code>标签的 rel 属性：**。其属性值有以下两种：</p>
<ul>
<li><code>stylesheet</code>：定义的样式表</li>
<li><code>alternate stylesheet</code>：候选的样式表</li>
</ul>
<p>看字面意思可能比较难理解，我们来举个例子，一下子就明白了。<br>举例：</p>
<p>现在我们来定义 3 个样式表：</p>
<p>a.css：定义一个实线的黑色边框</p>
<pre class="highlight"><span class="line"><span class="selector-tag">div</span> &#123;</span><br><span class="line">    <span class="attribute">width</span>: <span class="number">200px</span>;</span><br><span class="line">    <span class="attribute">height</span>: <span class="number">200px</span>;</span><br><span class="line">    <span class="attribute">border</span>: <span class="number">3px</span> solid black;</span><br><span class="line">&#125;</span><br></pre>

<p>ba.css：蓝色的虚线边框</p>
<pre class="highlight"><span class="line"><span class="selector-tag">div</span> &#123;</span><br><span class="line">    <span class="attribute">width</span>: <span class="number">200px</span>;</span><br><span class="line">    <span class="attribute">height</span>: <span class="number">200px</span>;</span><br><span class="line">    <span class="attribute">border</span>: <span class="number">3px</span> dotted blue;</span><br><span class="line">&#125;</span><br></pre>

<p>c.css：来个背景图片</p>
<pre class="highlight"><span class="line"><span class="selector-tag">div</span> &#123;</span><br><span class="line">    <span class="attribute">width</span>: <span class="number">200px</span>;</span><br><span class="line">    <span class="attribute">height</span>: <span class="number">200px</span>;</span><br><span class="line">    <span class="attribute">border</span>: <span class="number">3px</span> solid red;</span><br><span class="line">    <span class="attribute">background-image</span>: <span class="built_in">url</span>(<span class="string">&#x27;1.jpg&#x27;</span>);</span><br><span class="line">&#125;</span><br></pre>

<p>然后我们在 html 文件中引用三个样式表：</p>
<pre class="highlight"><span class="line"><span class="tag">&lt;<span class="name">link</span> <span class="attr">rel</span> = <span class="string">&quot;stylesheet&quot;</span> <span class="attr">type</span> = <span class="string">&quot;text/css&quot;</span> <span class="attr">href</span> = <span class="string">&quot;a.css&quot;</span>&gt;</span><span class="tag">&lt;/<span class="name">link</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">link</span> <span class="attr">rel</span> = <span class="string">&quot;alternate stylesheet&quot;</span> <span class="attr">type</span> = <span class="string">&quot;text/css&quot;</span> <span class="attr">href</span> = <span class="string">&quot;b.css&quot;</span> <span class="attr">title</span>=<span class="string">&quot;第二种样式&quot;</span>&gt;</span><span class="tag">&lt;/<span class="name">link</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">link</span> <span class="attr">rel</span> = <span class="string">&quot;alternate stylesheet&quot;</span> <span class="attr">type</span> = <span class="string">&quot;text/css&quot;</span> <span class="attr">href</span> = <span class="string">&quot;c.css&quot;</span> <span class="attr">title</span>=<span class="string">&quot;第三种样式&quot;</span>&gt;</span><span class="tag">&lt;/<span class="name">link</span>&gt;</span></span><br></pre>

<p>上面引入的三个样式表中，后面两个样式表作为备选。注意备选的样式表中，title 属性不要忘记写，不然显示不出来效果的。现在来看一下效果：（在 IE 中打开网页）</p>
<p><img  srcset="data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20300%20300'%3E%3C/svg%3E" data-src="http://img.smyhvae.com/2015-10-03-css-05.gif" class="lozad post-image"src="http://img.smyhvae.com/2015-10-03-css-05.gif"></p>
<h2 id="CSS-的四种基本选择器"><a href="#CSS-的四种基本选择器" class="headerlink" title="CSS 的四种基本选择器"></a>CSS 的四种基本选择器</h2><p>CSS 选择器：就是指定 CSS 要作用的标签，那个标签的名称就是选择器。意为：选择哪个容器。</p>
<p>CSS 的选择器分为两大类：基本选择器和扩展选择器。</p>
<p><strong>基本选择器：</strong></p>
<ul>
<li>标签选择器：针对<strong>一类</strong>标签</li>
<li>ID 选择器：针对某<strong>一个</strong>特定的标签使用</li>
<li>类选择器：针对<strong>你想要的所有</strong>标签使用</li>
<li>通用选择器（通配符）：针对所有的标签都适用（不建议使用）</li>
</ul>
<p>下面来分别讲一讲。</p>
<h3 id="1、标签选择器：选择器的名字代表-html-页面上的标签"><a href="#1、标签选择器：选择器的名字代表-html-页面上的标签" class="headerlink" title="1、标签选择器：选择器的名字代表 html 页面上的标签"></a>1、标签选择器：选择器的名字代表 html 页面上的标签</h3><p>标签选择器，选择的是页面上所有这种类型的标签，所以经常描述“<strong>共性</strong>”，无法描述某一个元素的“个性”。</p>
<p>举例：</p>
<pre class="highlight"><span class="line">p&#123; font-size:14px; &#125;</span><br></pre>

<p>上方选择器的意思是说：所有的<code>&lt;p&gt;</code>标签里的内容都将显示 14 号字体。</p>
<p>效果：</p>
<p><img  srcset="data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20300%20300'%3E%3C/svg%3E" data-src="http://img.smyhvae.com/2015-10-03-css-06.png" class="lozad post-image"src="http://img.smyhvae.com/2015-10-03-css-06.png"></p>
<p>再比如说，我想让“千古壹号学完了安卓，继续学前端哟”这句话中的“前端”两个变为红色字体，那么我可以用<code>&lt;span&gt;</code>标签把“前端”这两个字围起来，然后给<code>&lt;span&gt;</code>标签加一个标签选择器。</p>
<p>代码如下：</p>
<pre class="highlight"><span class="line"><span class="meta">&lt;!DOCTYPE <span class="keyword">html</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">html</span> <span class="attr">lang</span>=<span class="string">&quot;en&quot;</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">head</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">meta</span> <span class="attr">charset</span>=<span class="string">&quot;UTF-8&quot;</span> /&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">title</span>&gt;</span>Document<span class="tag">&lt;/<span class="name">title</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">style</span> <span class="attr">type</span>=<span class="string">&quot;text/css&quot;</span>&gt;</span><span class="language-css"></span></span><br><span class="line"><span class="language-css">            <span class="selector-tag">span</span> &#123;</span></span><br><span class="line"><span class="language-css">                <span class="attribute">color</span>: red;</span></span><br><span class="line"><span class="language-css">            &#125;</span></span><br><span class="line"><span class="language-css">        </span><span class="tag">&lt;/<span class="name">style</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">head</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">body</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">p</span>&gt;</span>千古壹号学完了安卓，继续学<span class="tag">&lt;<span class="name">span</span>&gt;</span>前端<span class="tag">&lt;/<span class="name">span</span>&gt;</span>哟<span class="tag">&lt;/<span class="name">p</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">body</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">html</span>&gt;</span></span><br></pre>

<p>【总结】需要注意的是：</p>
<p>（1）所有的标签，都可以是选择器。比如 ul、li、label、dt、dl、input。</p>
<p>（2）无论这个标签藏的多深，一定能够被选择上。</p>
<p>（3）选择的所有，而不是一个。</p>
<h3 id="2、ID-选择器：规定用-来定义"><a href="#2、ID-选择器：规定用-来定义" class="headerlink" title="2、ID 选择器：规定用#来定义"></a>2、ID 选择器：规定用<code>#</code>来定义</h3><p>针对某一个特定的标签来使用，只能使用一次。css 中的 ID 选择器以”#”来定义。</p>
<p>举例：</p>
<pre class="highlight"><span class="line">#mytitle&#123; border:3px dashed green; &#125;</span><br></pre>

<p>效果：</p>
<p><img  srcset="data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20300%20300'%3E%3C/svg%3E" data-src="http://img.smyhvae.com/2015-10-03-css-08.png" class="lozad post-image"src="http://img.smyhvae.com/2015-10-03-css-08.png"></p>
<p>id 选择器的选择符是“#”。</p>
<p>任何的 HTML 标签都可以有 id 属性。表示这个标签的名字。这个标签的名字，可以任取，但是：</p>
<ul>
<li>（1）只能有字母、数字、下划线。</li>
<li>（2）必须以字母开头。</li>
<li>（3）不能和标签同名。比如 id 不能叫做 body、img、a。</li>
</ul>
<p>另外，特别强调的是：<strong>HTML 页面，不能出现相同的 id，哪怕他们不是一个类型</strong>。比如页面上有一个 id 为 pp 的 p，一个 id 为 pp 的 div，是非法的！</p>
<p><strong>一个标签可以被多个 css 选择器选择：</strong></p>
<p>比如，我们可以同时让标签选择器和 id 选择器作用于同一个标签。如下：</p>
<p><img  srcset="data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20300%20300'%3E%3C/svg%3E" data-src="http://img.smyhvae.com/20170710_1737.png" class="lozad post-image"src="http://img.smyhvae.com/20170710_1737.png"></p>
<p>然后我们通过网页的审查元素看一下效果：</p>
<p><img  srcset="data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20300%20300'%3E%3C/svg%3E" data-src="http://img.smyhvae.com/20170711_1540.png" class="lozad post-image"src="http://img.smyhvae.com/20170711_1540.png"></p>
<p>现在，假设选择器冲突了，比如 id 选择器说这个文字是红色的，标签选择器说这个文字是绿色的。那么听谁的？<br>实际上，css 有着非常严格的计算公式，能够处理冲突.</p>
<p>一个标签可以被多个 css 选择器选择，共同作用，这就是“<strong>层叠式</strong>”的第一层含义（第一层含义和第二层含义，放到 css 基础的第三篇文章里讲）。</p>
<h3 id="3、类选择器：规定用圆点-来定义"><a href="#3、类选择器：规定用圆点-来定义" class="headerlink" title="3、类选择器：规定用圆点.来定义"></a>3、类选择器：规定用圆点<code>.</code>来定义</h3><p>、针对<strong>你想要的所有</strong>标签使用。优点：灵活。</p>
<p>css 中用<code>.</code>来表示类。举例如下：</p>
<pre class="highlight"><span class="line">.one&#123; width:800px; &#125;</span><br></pre>

<p>效果：</p>
<p><img  srcset="data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20300%20300'%3E%3C/svg%3E" data-src="http://img.smyhvae.com/2015-10-03-css-07.png" class="lozad post-image"src="http://img.smyhvae.com/2015-10-03-css-07.png"></p>
<p>和 id 非常相似，任何的标签都可以携带 id 属性和 class 属性。class 属性的特点：</p>
<ul>
<li><p>特性 1：类选择器可以被多种标签使用。</p>
</li>
<li><p>特性 2：同一个标签可以使用多个类选择器。用<strong>空格</strong>隔开。举例如下：（正确）</p>
</li>
</ul>
<pre class="highlight"><span class="line"><span class="tag">&lt;<span class="name">h3</span> <span class="attr">class</span>=<span class="string">&quot;teshu  zhongyao&quot;</span>&gt;</span>我是一个h3啊<span class="tag">&lt;/<span class="name">h3</span>&gt;</span></span><br></pre>

<p>初学者常见的错误，就是写成了两个 class。举例如下：（错误）</p>
<pre class="highlight"><span class="line"><span class="tag">&lt;<span class="name">h3</span> <span class="attr">class</span>=<span class="string">&quot;teshu&quot;</span> <span class="attr">class</span>=<span class="string">&quot;zhongyao&quot;</span>&gt;</span>我是一个h3啊<span class="tag">&lt;/<span class="name">h3</span>&gt;</span></span><br></pre>

<p><strong>类选择器使用的举例：</strong></p>
<p>类选择器的使用，能够决定一个人的 css 水平。</p>
<p>比如，我们现在要做下面这样一个页面：</p>
<p><img  srcset="data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20300%20300'%3E%3C/svg%3E" data-src="http://img.smyhvae.com/20170711_1639.png" class="lozad post-image"src="http://img.smyhvae.com/20170711_1639.png"></p>
<p>正确的思路，就是用所谓“公共类”的思路，就是我们类就是提供“公共服务”，比如有绿、大、线，一旦携带这个类名，就有相应的样式变化。对应 css 里的代码如下：</p>
<pre class="highlight"><span class="line"><span class="tag">&lt;<span class="name">style</span> <span class="attr">type</span>=<span class="string">&quot;text/css&quot;</span>&gt;</span><span class="language-css"></span></span><br><span class="line"><span class="language-css">    <span class="selector-class">.lv</span> &#123;</span></span><br><span class="line"><span class="language-css">        <span class="attribute">color</span>: green;</span></span><br><span class="line"><span class="language-css">    &#125;</span></span><br><span class="line"><span class="language-css">    <span class="selector-class">.da</span> &#123;</span></span><br><span class="line"><span class="language-css">        <span class="attribute">font-size</span>: <span class="number">30px</span>;</span></span><br><span class="line"><span class="language-css">    &#125;</span></span><br><span class="line"><span class="language-css">    <span class="selector-class">.underline</span> &#123;</span></span><br><span class="line"><span class="language-css">        <span class="attribute">text-decoration</span>: underline;</span></span><br><span class="line"><span class="language-css">    &#125;</span></span><br><span class="line"><span class="language-css"></span><span class="tag">&lt;/<span class="name">style</span>&gt;</span></span><br></pre>

<p>然后让每个标签去选取自己想要用的类选择器：</p>
<pre class="highlight"><span class="line"><span class="tag">&lt;<span class="name">p</span> <span class="attr">class</span>=<span class="string">&quot;lv da&quot;</span>&gt;</span>段落1<span class="tag">&lt;/<span class="name">p</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">p</span> <span class="attr">class</span>=<span class="string">&quot;lv xian&quot;</span>&gt;</span>段落2<span class="tag">&lt;/<span class="name">p</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">p</span> <span class="attr">class</span>=<span class="string">&quot;da xian&quot;</span>&gt;</span>段落3<span class="tag">&lt;/<span class="name">p</span>&gt;</span></span><br></pre>

<p>也就是说：</p>
<p>（1）不要去试图用一个类名，把某个标签的所有样式写完。这个标签要多携带几个类，共同完成这个标签的样式。</p>
<p>（2）每一个类要尽可能小，有“公共”的概念，能够让更多的标签使用。</p>
<p>问题：到底用 id 还是用 class？</p>
<p>答案：尽可能的用 class，除非极特殊的情况可以用 id。</p>
<p>原因：id 是 js 用的。也就是说，js 要通过 id 属性得到标签，所以 css 层面尽量不用 id，要不然 js 就很别扭。另一层面，我们会认为一个有 id 的元素，有动态效果。</p>
<p>举例如下：</p>
<p><img  srcset="data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20300%20300'%3E%3C/svg%3E" data-src="http://img.smyhvae.com/20170711_1706.png" class="lozad post-image"src="http://img.smyhvae.com/20170711_1706.png"></p>
<p>上图所示，css 和 js 都在用同一个 id，会出现不好沟通的情况。</p>
<p>我们记住这句话：<strong>类上样式，id 上行为</strong>。意思是说，<code>class</code>属性交给 css 使用，<code>id</code>属性交给 js 使用。</p>
<p><strong>上面这三种选择器的区别：</strong></p>
<ul>
<li>标签选择器针对的是页面上的一类标签。</li>
<li>ID 选择器是只针对特定的标签(一个)，ID 是此标签在此页面上的唯一标识。</li>
<li>类选择器可以被多种标签使用。</li>
</ul>
<h3 id="4、通配符-：匹配任何标签"><a href="#4、通配符-：匹配任何标签" class="headerlink" title="4、通配符*：匹配任何标签"></a>4、通配符<code>*</code>：匹配任何标签</h3><p>通用选择器，将匹配任何标签。不建议使用，IE 有些版本不支持，大网站增加客户端负担。</p>
<p>效率不高，如果页面上的标签越多，效率越低，所以页面上不能出现这个选择器。</p>
<p>举例：</p>
<pre class="highlight"><span class="line">* &#123;</span><br><span class="line">    <span class="attribute">margin-left</span>: <span class="number">0px</span>;</span><br><span class="line">    <span class="attribute">margin-top</span>: <span class="number">0px</span>;</span><br><span class="line">&#125;</span><br></pre>

<p>效果：</p>
<p><img  srcset="data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20300%20300'%3E%3C/svg%3E" data-src="http://img.smyhvae.com/2015-10-03-css-09.png" class="lozad post-image"src="http://img.smyhvae.com/2015-10-03-css-09.png"></p>
<h2 id="CSS-的几种高级选择器"><a href="#CSS-的几种高级选择器" class="headerlink" title="CSS 的几种高级选择器"></a>CSS 的几种高级选择器</h2><p><strong>高级选择器：</strong></p>
<ul>
<li>后代选择器：用空格隔开</li>
<li>交集选择器：选择器之间紧密相连</li>
<li>并集选择器（分组选择器）：用逗号隔开</li>
<li>伪类选择器</li>
</ul>
<p>下面详细讲一下这几种高级（扩展）选择器。</p>
<h3 id="1、后代选择器-定义的时候用空格隔开"><a href="#1、后代选择器-定义的时候用空格隔开" class="headerlink" title="1、后代选择器: 定义的时候用空格隔开"></a>1、后代选择器: 定义的时候用空格隔开</h3><p>对于<code>E F</code>这种格式，表示<strong>所有属于 E 元素后代的 F 元素</strong>，有这个样式。空格就表示后代。</p>
<p>后代选择器，就是一种平衡：共性、特性的平衡。当要把<strong>某一个部分的所有的什么</strong>，进行样式改变，就要想到后代选择器。</p>
<p>后代选择器，描述的是祖先结构。</p>
<p>看定义可能有点难理解，我们来看例子吧。</p>
<p>举例 1：</p>
<pre class="highlight"><span class="line"><span class="tag">&lt;<span class="name">style</span> <span class="attr">type</span>=<span class="string">&quot;text/css&quot;</span>&gt;</span><span class="language-css"></span></span><br><span class="line"><span class="language-css">    <span class="selector-class">.div1</span> <span class="selector-tag">p</span> &#123;</span></span><br><span class="line"><span class="language-css">        <span class="attribute">color</span>: red;</span></span><br><span class="line"><span class="language-css">    &#125;</span></span><br><span class="line"><span class="language-css"></span><span class="tag">&lt;/<span class="name">style</span>&gt;</span></span><br></pre>

<p>空格就表示后代。<code>.div1 p</code> 表示<code>.div1</code>的后代所有的<code>p</code>。</p>
<p>这里强调一下：这两个标签不一定是连续紧挨着的，只要保持一个后代的关联即可。也就是说，选择的是后代，不一定是儿子。</p>
<p>举例：</p>
<pre class="highlight"><span class="line"><span class="tag">&lt;<span class="name">style</span> <span class="attr">type</span>=<span class="string">&quot;text/css&quot;</span>&gt;</span><span class="language-css"></span></span><br><span class="line"><span class="language-css">    <span class="selector-tag">h3</span> <span class="selector-tag">b</span> <span class="selector-tag">i</span> &#123;</span></span><br><span class="line"><span class="language-css">        <span class="attribute">color</span>: red;</span></span><br><span class="line"><span class="language-css">    &#125;</span></span><br><span class="line"><span class="language-css"></span><span class="tag">&lt;/<span class="name">style</span>&gt;</span></span><br></pre>

<p>上方代码的意思是说：定义了<code>&lt;h3&gt;</code>标签中的<code>&lt;b&gt;</code>标签中的<code>&lt;i&gt;</code>标签的样式。<br>同理：h3 和 b 和 i 标签不一定是连续紧挨着的，只要保持一个后代的关联即可。</p>
<p>效果：</p>
<p><img  srcset="data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20300%20300'%3E%3C/svg%3E" data-src="http://img.smyhvae.com/2015-10-03-css-11.png" class="lozad post-image"src="http://img.smyhvae.com/2015-10-03-css-11.png"></p>
<p>或者还有下面这种写法：</p>
<p><img  srcset="data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20300%20300'%3E%3C/svg%3E" data-src="http://img.smyhvae.com/2015-10-03-css-12.png" class="lozad post-image"src="http://img.smyhvae.com/2015-10-03-css-12.png"></p>
<p>上面的这种写法，<code>&lt;h3&gt;</code>标签和<code>&lt;i&gt;</code>标签并不是紧挨着的，但他们保持着一种后代关系。</p>
<p>还有下面这种写法：（含类选择器、id 选择器都是可以的）</p>
<p><img  srcset="data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20300%20300'%3E%3C/svg%3E" data-src="http://img.smyhvae.com/2015-10-03-css-13.png" class="lozad post-image"src="http://img.smyhvae.com/2015-10-03-css-13.png"></p>
<p>我们在开头说了：<strong>后代选择器，描述的是一种祖先结构</strong>。我们举个例子来说明这句话：</p>
<pre class="highlight"><span class="line"><span class="meta">&lt;!DOCTYPE <span class="keyword">html</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">html</span> <span class="attr">lang</span>=<span class="string">&quot;en&quot;</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">head</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">meta</span> <span class="attr">charset</span>=<span class="string">&quot;UTF-8&quot;</span> /&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">title</span>&gt;</span>Document<span class="tag">&lt;/<span class="name">title</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">style</span> <span class="attr">type</span>=<span class="string">&quot;text/css&quot;</span>&gt;</span><span class="language-css"></span></span><br><span class="line"><span class="language-css">            <span class="selector-tag">div</span> <span class="selector-tag">div</span> <span class="selector-tag">p</span> &#123;</span></span><br><span class="line"><span class="language-css">                <span class="attribute">color</span>: red;</span></span><br><span class="line"><span class="language-css">            &#125;</span></span><br><span class="line"><span class="language-css">        </span><span class="tag">&lt;/<span class="name">style</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">head</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">body</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">div</span>&gt;</span></span><br><span class="line">            <span class="tag">&lt;<span class="name">div</span> <span class="attr">class</span>=<span class="string">&quot;div2&quot;</span>&gt;</span></span><br><span class="line">                <span class="tag">&lt;<span class="name">div</span> <span class="attr">class</span>=<span class="string">&quot;div3&quot;</span>&gt;</span></span><br><span class="line">                    <span class="tag">&lt;<span class="name">div</span> <span class="attr">class</span>=<span class="string">&quot;div4&quot;</span>&gt;</span></span><br><span class="line">                        <span class="tag">&lt;<span class="name">p</span>&gt;</span>我是什么颜色？<span class="tag">&lt;/<span class="name">p</span>&gt;</span></span><br><span class="line">                    <span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line">                <span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line">            <span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">body</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">html</span>&gt;</span></span><br></pre>

<p>上面 css 中的<code>div div p</code>，也能使文字的颜色变红。通过浏览器的审查元素，我们可以看到 p 元素的祖先列表：</p>
<p><img  srcset="data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20300%20300'%3E%3C/svg%3E" data-src="http://img.smyhvae.com/20170711_1836.png" class="lozad post-image"src="http://img.smyhvae.com/20170711_1836.png"></p>
<p>讲到这里，我们再提一个 VS Code 的快捷键：</p>
<p>在 VS Code 中输入<code>p#haha</code>，按 tab 键后，会生成<code>&lt;p id=&quot;haha&quot;&gt;&lt;/p&gt;</code>。</p>
<p>在 VS Code 中输入<code>p.haha</code>，按 tab 键后，会生成<code>&lt;p class=&quot;haha&quot;&gt;&lt;/p&gt;</code>。</p>
<h3 id="2、交集选择器：定义的时候紧密相连"><a href="#2、交集选择器：定义的时候紧密相连" class="headerlink" title="2、交集选择器：定义的时候紧密相连"></a>2、交集选择器：定义的时候紧密相连</h3><p>定义交集选择器的时候，两个选择器之间紧密相连。一般是以标签名开头，比如<code>div.haha</code>，再比如<code>p.special</code>。</p>
<p>如果后一个选择器是类选择器，则写为<code>div.special</code>；如果后一个选择器 id 选择器，则写为<code>div#special</code>。</p>
<p>来看下面这张图就明白了：</p>
<p><img  srcset="data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20300%20300'%3E%3C/svg%3E" data-src="http://img.smyhvae.com/20170711_1851.png" class="lozad post-image"src="http://img.smyhvae.com/20170711_1851.png"></p>
<pre class="highlight"><span class="line"><span class="selector-tag">h3</span><span class="selector-class">.special</span> &#123;</span><br><span class="line">    <span class="attribute">color</span>: red;</span><br><span class="line">&#125;</span><br></pre>

<p>选择的元素要求同时满足两个条件：必须是 h3 标签，然后必须是 special 标签。</p>
<p>举例：</p>
<pre class="highlight"><span class="line"><span class="meta">&lt;!DOCTYPE <span class="keyword">html</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">html</span> <span class="attr">lang</span>=<span class="string">&quot;en&quot;</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">head</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">meta</span> <span class="attr">charset</span>=<span class="string">&quot;UTF-8&quot;</span> /&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">title</span>&gt;</span>交集选择器测试<span class="tag">&lt;/<span class="name">title</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">style</span> <span class="attr">type</span>=<span class="string">&quot;text/css&quot;</span>&gt;</span><span class="language-css"></span></span><br><span class="line"><span class="language-css">            <span class="selector-tag">h3</span><span class="selector-class">.special</span> &#123;</span></span><br><span class="line"><span class="language-css">                <span class="attribute">color</span>: red;</span></span><br><span class="line"><span class="language-css">            &#125;</span></span><br><span class="line"><span class="language-css">        </span><span class="tag">&lt;/<span class="name">style</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">head</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">body</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">h3</span> <span class="attr">class</span>=<span class="string">&quot;special zhongyao&quot;</span>&gt;</span>标题1<span class="tag">&lt;/<span class="name">h3</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">h3</span> <span class="attr">class</span>=<span class="string">&quot;special&quot;</span>&gt;</span>我也是标题<span class="tag">&lt;/<span class="name">h3</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">p</span>&gt;</span>我是段落<span class="tag">&lt;/<span class="name">p</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">body</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">html</span>&gt;</span></span><br></pre>

<p>效果如下：</p>
<p><img  srcset="data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20300%20300'%3E%3C/svg%3E" data-src="http://img.smyhvae.com/20170711_1852.png" class="lozad post-image"src="http://img.smyhvae.com/20170711_1852.png"></p>
<p>注意，交集选择器没有空格。所以，没有空格的<code>div.red</code>（交集选择器）和有空格的<code>div .red</code>（后代选择器）不是一个意思。</p>
<p>交集选择器可以连续交：（一般不要这么写）</p>
<pre class="highlight"><span class="line"><span class="selector-tag">h3</span><span class="selector-class">.special</span><span class="selector-class">.zhongyao</span> &#123;</span><br><span class="line">    <span class="attribute">color</span>: red;</span><br><span class="line">&#125;</span><br></pre>

<p>上面这种写法，是 IE7 开始兼容的，IE6 不兼容。</p>
<h3 id="3、并集选择器：定义的时候用逗号隔开"><a href="#3、并集选择器：定义的时候用逗号隔开" class="headerlink" title="3、并集选择器：定义的时候用逗号隔开"></a>3、并集选择器：定义的时候用逗号隔开</h3><p>三种基本选择器都可以放进来。</p>
<p>举例：</p>
<pre class="highlight"><span class="line"><span class="selector-tag">p</span>,<span class="selector-tag">h1</span>,<span class="selector-class">.title1</span>,<span class="selector-id">#one</span> &#123;</span><br><span class="line">    <span class="attribute">color</span>: red;</span><br><span class="line">&#125;</span><br></pre>

<p>效果：</p>
<p><img  srcset="data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20300%20300'%3E%3C/svg%3E" data-src="https://img.smyhvae.com/20211116_1055.png" class="lozad post-image"src="https://img.smyhvae.com/20211116_1055.png"></p>
<h2 id="一些-CSS3-选择器"><a href="#一些-CSS3-选择器" class="headerlink" title="一些 CSS3 选择器"></a>一些 CSS3 选择器</h2><blockquote>
<p>所有的 CSS3 选择器，我们放在 CSS3 的内容里介绍。本文暂时先接触一部分。</p>
</blockquote>
<h3 id="浏览器的兼容性问题"><a href="#浏览器的兼容性问题" class="headerlink" title="浏览器的兼容性问题"></a>浏览器的兼容性问题</h3><blockquote>
<p>我们可以用<code>IETester</code>这个软件测一下 CSS 在各个版本 IE 浏览器上的显示效果。</p>
</blockquote>
<p>IE： 微软的浏览器，随着操作系统安装的。所以每个 windows 都有 IE 浏览器。各版本如下：</p>
<ul>
<li>windows xp 操作系统安装的 IE6</li>
<li>windows vista 操作系统安装的 IE7</li>
<li>windows 7 操作系统安装的 IE8</li>
<li>windows 8 操作系统安装的 IE9</li>
<li>windows10 操作系统安装的 edge</li>
</ul>
<p>浏览器兼容问题，要出，就基本上就是出在 IE6、7 身上，这两个浏览器是非常低级的浏览器。</p>
<p>为了测试浏览器 CSS 3 的兼容性，我们可以在网上搜”css3 机器猫”关键字，然后在不同的浏览器中打开如下链接：</p>
<ul>
<li><a target="_blank" rel="noopener" href="http://www1.pconline.com.cn/pcedu/specialtopic/css3-doraemon/">http://www1.pconline.com.cn/pcedu/specialtopic/css3-doraemon/</a></li>
</ul>
<p>测试结果如下：</p>
<p><img  srcset="data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20300%20300'%3E%3C/svg%3E" data-src="http://img.smyhvae.com/20170711_1939.png" class="lozad post-image"src="http://img.smyhvae.com/20170711_1939.png"></p>
<p>我们可以在<a target="_blank" rel="noopener" href="http://tongji.baidu.com/data/">百度统计</a>里查看浏览器的市场占有率：</p>
<ul>
<li>IE9 5.94%</li>
<li>IE8 21.19%</li>
<li>IE7 4.79%</li>
<li>IE6 4.11%</li>
</ul>
<p>我们可以在<a target="_blank" rel="noopener" href="http://html5test.com/results/desktop.html">http://html5test.com/results/desktop.html</a>中查看</p>
<p><img  srcset="data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20300%20300'%3E%3C/svg%3E" data-src="http://img.smyhvae.com/20170711_1948.png" class="lozad post-image"src="http://img.smyhvae.com/20170711_1948.png"></p>
<p>我们要知道典型的 IE6 兼容问题（面试要问），但是做项目我们兼容到 IE8 即可。不解决 IE8 以下的兼容问题，目的在于：培养更高的兴趣和眼光，别天天的跟 IE6 较劲。</p>
<p>我们可以用「IETester」软件看看 css 在各个浏览器中的显示效果。</p>
<h3 id="1-子代选择器，用符号-gt-表示"><a href="#1-子代选择器，用符号-gt-表示" class="headerlink" title="1.子代选择器，用符号&gt;表示"></a>1.子代选择器，用符号<code>&gt;</code>表示</h3><blockquote>
<p>IE7 开始兼容，IE6 不兼容。</p>
</blockquote>
<pre class="highlight"><span class="line"><span class="selector-tag">div</span> &gt; <span class="selector-tag">p</span> &#123;</span><br><span class="line">    <span class="attribute">color</span>: red;</span><br><span class="line">&#125;</span><br></pre>

<p>div 的儿子 p。和 div 的后代 p 的截然不同。</p>
<p>能够选择：</p>
<pre class="highlight"><span class="line"><span class="tag">&lt;<span class="name">div</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">p</span>&gt;</span>我是div的儿子<span class="tag">&lt;/<span class="name">p</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br></pre>

<p>不能选择：</p>
<pre class="highlight"><span class="line"><span class="tag">&lt;<span class="name">div</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">ul</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">li</span>&gt;</span></span><br><span class="line">            <span class="tag">&lt;<span class="name">p</span>&gt;</span>我是div的重孙子<span class="tag">&lt;/<span class="name">p</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;/<span class="name">li</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">ul</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br></pre>

<h3 id="2-序选择器"><a href="#2-序选择器" class="headerlink" title="2.序选择器"></a>2.序选择器</h3><blockquote>
<p>IE8 开始兼容；IE6、7 都不兼容</p>
</blockquote>
<p>设置无序列表<code>&lt;ul&gt;</code>中的第一个<code>&lt;li&gt;</code>为红色：</p>
<pre class="highlight"><span class="line"><span class="tag">&lt;<span class="name">style</span> <span class="attr">type</span>=<span class="string">&quot;text/css&quot;</span>&gt;</span><span class="language-css"></span></span><br><span class="line"><span class="language-css">    <span class="selector-tag">ul</span> <span class="selector-tag">li</span><span class="selector-pseudo">:first</span>-child &#123;</span></span><br><span class="line"><span class="language-css">        <span class="attribute">color</span>: red;</span></span><br><span class="line"><span class="language-css">    &#125;</span></span><br><span class="line"><span class="language-css"></span><span class="tag">&lt;/<span class="name">style</span>&gt;</span></span><br></pre>

<p>设置无序列表<code>&lt;ul&gt;</code>中的最后一个<code>&lt;li&gt;</code>为红色：</p>
<pre class="highlight"><span class="line"><span class="selector-tag">ul</span> <span class="selector-tag">li</span><span class="selector-pseudo">:last-child</span> &#123;</span><br><span class="line">    <span class="attribute">color</span>: blue;</span><br><span class="line">&#125;</span><br></pre>

<p>序选择器还有更复杂的用法，以后再讲。</p>
<p>由于浏览器的更新需要过程，所以现在如果公司还要求兼容 IE6、7，那么就要自己写类名：</p>
<pre class="highlight"><span class="line"><span class="tag">&lt;<span class="name">ul</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">li</span> <span class="attr">class</span>=<span class="string">&quot;first&quot;</span>&gt;</span>项目<span class="tag">&lt;/<span class="name">li</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">li</span>&gt;</span>项目<span class="tag">&lt;/<span class="name">li</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">li</span>&gt;</span>项目<span class="tag">&lt;/<span class="name">li</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">li</span>&gt;</span>项目<span class="tag">&lt;/<span class="name">li</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">li</span>&gt;</span>项目<span class="tag">&lt;/<span class="name">li</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">li</span>&gt;</span>项目<span class="tag">&lt;/<span class="name">li</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">li</span>&gt;</span>项目<span class="tag">&lt;/<span class="name">li</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">li</span>&gt;</span>项目<span class="tag">&lt;/<span class="name">li</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">li</span>&gt;</span>项目<span class="tag">&lt;/<span class="name">li</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">li</span> <span class="attr">class</span>=<span class="string">&quot;last&quot;</span>&gt;</span>项目<span class="tag">&lt;/<span class="name">li</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">ul</span>&gt;</span></span><br></pre>

<p>用类选择器来选择第一个或者最后一个：</p>
<pre class="highlight"><span class="line">ul li.first&#123; color:red; &#125; ul li.last&#123; color:blue; &#125;</span><br></pre>

<h3 id="3-下一个兄弟选择器"><a href="#3-下一个兄弟选择器" class="headerlink" title="3.下一个兄弟选择器"></a>3.下一个兄弟选择器</h3><blockquote>
<p>IE7 开始兼容，IE6 不兼容。</p>
</blockquote>
<p><code>+</code>表示选择下一个兄弟</p>
<pre class="highlight"><span class="line"><span class="tag">&lt;<span class="name">style</span> <span class="attr">type</span>=<span class="string">&quot;text/css&quot;</span>&gt;</span><span class="language-css"></span></span><br><span class="line"><span class="language-css">    <span class="selector-tag">h3</span> + <span class="selector-tag">p</span> &#123;</span></span><br><span class="line"><span class="language-css">        <span class="attribute">color</span>: red;</span></span><br><span class="line"><span class="language-css">    &#125;</span></span><br><span class="line"><span class="language-css"></span><span class="tag">&lt;/<span class="name">style</span>&gt;</span></span><br></pre>

<p>上方的选择器意思是：选择的是 h3 元素后面紧挨着的第一个兄弟。</p>
<pre class="highlight"><span class="line"><span class="tag">&lt;<span class="name">h3</span>&gt;</span>我是一个标题<span class="tag">&lt;/<span class="name">h3</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">p</span>&gt;</span>我是一个段落<span class="tag">&lt;/<span class="name">p</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">p</span>&gt;</span>我是一个段落<span class="tag">&lt;/<span class="name">p</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">p</span>&gt;</span>我是一个段落<span class="tag">&lt;/<span class="name">p</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">h3</span>&gt;</span>我是一个标题<span class="tag">&lt;/<span class="name">h3</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">p</span>&gt;</span>我是一个段落<span class="tag">&lt;/<span class="name">p</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">p</span>&gt;</span>我是一个段落<span class="tag">&lt;/<span class="name">p</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">p</span>&gt;</span>我是一个段落<span class="tag">&lt;/<span class="name">p</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">h3</span>&gt;</span>我是一个标题<span class="tag">&lt;/<span class="name">h3</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">p</span>&gt;</span>我是一个段落<span class="tag">&lt;/<span class="name">p</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">p</span>&gt;</span>我是一个段落<span class="tag">&lt;/<span class="name">p</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">p</span>&gt;</span>我是一个段落<span class="tag">&lt;/<span class="name">p</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">h3</span>&gt;</span>我是一个标题<span class="tag">&lt;/<span class="name">h3</span>&gt;</span></span><br></pre>

<p>效果如下：</p>
<p><img  srcset="data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20300%20300'%3E%3C/svg%3E" data-src="http://img.smyhvae.com/20170711_1950.png" class="lozad post-image"src="http://img.smyhvae.com/20170711_1950.png"></p>
<p>这种选择器作用不大。</p>
<h2 id="我的公众号"><a href="#我的公众号" class="headerlink" title="我的公众号"></a>我的公众号</h2><p>想学习<strong>更多技能</strong>？不妨关注我的微信公众号：<strong>千古壹号</strong>（id：<code>qianguyihao</code>）。</p>
<p>扫一扫，你将发现另一个全新的世界，而这将是一场美丽的意外：</p>
<p><img  srcset="data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20300%20300'%3E%3C/svg%3E" data-src="http://img.smyhvae.com/20190101.png" class="lozad post-image"src="http://img.smyhvae.com/20190101.png"></p>

  </div>
  <div>
    
      <div 
        class="post-note note-warning copyright" 
        style="margin-top: 42px">
        <p>
          <span style="font-weight: bold;">作者：</span><a 
            target="_blank" 
            rel="nofollow noopener noreferrer" 
            href="/about">
            zwl5670
          </a>
        </p>
        <p>
          <span style="font-weight: bold;">文章链接：</span><a 
            target="_blank" 
            rel="nofollow noopener noreferrer" 
            href="https://zwl5670.gitee.io/2002/01/01/02-CSS%E5%9F%BA%E7%A1%80/03-CSS%E6%A0%B7%E5%BC%8F%E8%A1%A8%E5%92%8C%E9%80%89%E6%8B%A9%E5%99%A8/">
            https://zwl5670.gitee.io/2002/01/01/02-CSS%E5%9F%BA%E7%A1%80/03-CSS%E6%A0%B7%E5%BC%8F%E8%A1%A8%E5%92%8C%E9%80%89%E6%8B%A9%E5%99%A8/
          </a>
        </p>
        <p><span style="font-weight: bold;">版权声明：</span>本博客所有文章除特别声明外，均采用<a target="_blank" rel="noopener" href="https://creativecommons.org/licenses/by-nc-sa/4.0/deed.zh">CC BY-NC-SA 4.0 协议</a>。转载请注明出处！</p>
      </div>
    
  </div>
</article>
<div class="nav">
  
    <div class="nav-item-prev">
      <a 
        href="/2002/01/01/02-CSS%E5%9F%BA%E7%A1%80/01-CSS%E5%B1%9E%E6%80%A7%EF%BC%9A%E5%AD%97%E4%BD%93%E5%B1%9E%E6%80%A7%E5%92%8C%E6%96%87%E6%9C%AC%E5%B1%9E%E6%80%A7/" 
        class="nav-link">
        <i class="iconfont icon-left nav-prev-icon"></i>
        <div>
          <div class="nav-label">上一篇</div>
          
            <div class="nav-title">01-CSS属性：字体属性和文本属性 </div>
          
        </div>
      </a>
    </div>
  
  
    <div class="nav-item-next">
      <a 
        href="/2002/01/01/02-CSS%E5%9F%BA%E7%A1%80/02-CSS%E5%B1%9E%E6%80%A7%EF%BC%9A%E8%83%8C%E6%99%AF%E5%B1%9E%E6%80%A7/" 
        class="nav-link">
        <div>
          <div class="nav-label">下一篇</div>
          
            <div class="nav-title">02-CSS属性：背景属性 </div>
          
        </div>
        <i class="iconfont icon-right nav-next-icon"></i>
      </a>
    </div>
  
</div>

<div 
  class="card card-content toc-card" 
  id="mobiletoc">
  <div class="toc-header">
  <i 
    class="iconfont icon-menu" 
    style="padding-right: 2px;">
  </i>目录
</div>
<ol class="toc"><li class="toc-item toc-level-2"><a class="toc-link" href="#%E6%9C%AC%E6%96%87%E4%B8%BB%E8%A6%81%E5%86%85%E5%AE%B9"><span class="toc-text">本文主要内容</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%89%8D%E8%A8%80"><span class="toc-text">前言</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#CSS-%E6%A6%82%E8%BF%B0"><span class="toc-text">CSS 概述</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#CSS-%E7%9A%84%E9%87%8D%E7%82%B9%E7%9F%A5%E8%AF%86%E7%82%B9"><span class="toc-text">CSS 的重点知识点</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#CSS-%E6%95%B4%E4%BD%93%E6%84%9F%E7%9F%A5"><span class="toc-text">CSS 整体感知</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#CSS-%E8%AF%AD%E6%B3%95"><span class="toc-text">CSS 语法</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#css-%E4%BB%A3%E7%A0%81%E7%9A%84%E6%B3%A8%E9%87%8A"><span class="toc-text">css 代码的注释</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#CSS-%E7%9A%84%E4%B8%80%E4%BA%9B%E7%AE%80%E5%8D%95%E5%B8%B8%E8%A7%81%E7%9A%84%E5%B1%9E%E6%80%A7"><span class="toc-text">CSS 的一些简单常见的属性</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#CSS-%E7%9A%84%E4%B9%A6%E5%86%99%E6%96%B9%E5%BC%8F"><span class="toc-text">CSS 的书写方式</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#1%E3%80%81CSS-%E5%92%8C-HTML-%E7%BB%93%E5%90%88%E6%96%B9%E5%BC%8F%E4%B8%80%EF%BC%9A%E8%A1%8C%E5%86%85%E6%A0%B7%E5%BC%8F"><span class="toc-text">1、CSS 和 HTML 结合方式一：行内样式</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#2%E3%80%81CSS-%E5%92%8C-HTML-%E7%BB%93%E5%90%88%E6%96%B9%E5%BC%8F%E4%BA%8C%EF%BC%9A%E5%86%85%E5%B5%8C%E6%A0%B7%E5%BC%8F%E8%A1%A8"><span class="toc-text">2、CSS 和 HTML 结合方式二：内嵌样式表</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#3%E3%80%81CSS-%E5%92%8C-HTML-%E7%BB%93%E5%90%88%E6%96%B9%E5%BC%8F%E4%B8%89%EF%BC%9A%E5%BC%95%E5%85%A5%E5%A4%96%E9%83%A8%E6%A0%B7%E5%BC%8F%E8%A1%A8-css-%E6%96%87%E4%BB%B6"><span class="toc-text">3、CSS 和 HTML 结合方式三：引入外部样式表 css 文件</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#CSS-%E7%9A%84%E5%9B%9B%E7%A7%8D%E5%9F%BA%E6%9C%AC%E9%80%89%E6%8B%A9%E5%99%A8"><span class="toc-text">CSS 的四种基本选择器</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#1%E3%80%81%E6%A0%87%E7%AD%BE%E9%80%89%E6%8B%A9%E5%99%A8%EF%BC%9A%E9%80%89%E6%8B%A9%E5%99%A8%E7%9A%84%E5%90%8D%E5%AD%97%E4%BB%A3%E8%A1%A8-html-%E9%A1%B5%E9%9D%A2%E4%B8%8A%E7%9A%84%E6%A0%87%E7%AD%BE"><span class="toc-text">1、标签选择器：选择器的名字代表 html 页面上的标签</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#2%E3%80%81ID-%E9%80%89%E6%8B%A9%E5%99%A8%EF%BC%9A%E8%A7%84%E5%AE%9A%E7%94%A8-%E6%9D%A5%E5%AE%9A%E4%B9%89"><span class="toc-text">2、ID 选择器：规定用#来定义</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#3%E3%80%81%E7%B1%BB%E9%80%89%E6%8B%A9%E5%99%A8%EF%BC%9A%E8%A7%84%E5%AE%9A%E7%94%A8%E5%9C%86%E7%82%B9-%E6%9D%A5%E5%AE%9A%E4%B9%89"><span class="toc-text">3、类选择器：规定用圆点.来定义</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#4%E3%80%81%E9%80%9A%E9%85%8D%E7%AC%A6-%EF%BC%9A%E5%8C%B9%E9%85%8D%E4%BB%BB%E4%BD%95%E6%A0%87%E7%AD%BE"><span class="toc-text">4、通配符*：匹配任何标签</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#CSS-%E7%9A%84%E5%87%A0%E7%A7%8D%E9%AB%98%E7%BA%A7%E9%80%89%E6%8B%A9%E5%99%A8"><span class="toc-text">CSS 的几种高级选择器</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#1%E3%80%81%E5%90%8E%E4%BB%A3%E9%80%89%E6%8B%A9%E5%99%A8-%E5%AE%9A%E4%B9%89%E7%9A%84%E6%97%B6%E5%80%99%E7%94%A8%E7%A9%BA%E6%A0%BC%E9%9A%94%E5%BC%80"><span class="toc-text">1、后代选择器: 定义的时候用空格隔开</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#2%E3%80%81%E4%BA%A4%E9%9B%86%E9%80%89%E6%8B%A9%E5%99%A8%EF%BC%9A%E5%AE%9A%E4%B9%89%E7%9A%84%E6%97%B6%E5%80%99%E7%B4%A7%E5%AF%86%E7%9B%B8%E8%BF%9E"><span class="toc-text">2、交集选择器：定义的时候紧密相连</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#3%E3%80%81%E5%B9%B6%E9%9B%86%E9%80%89%E6%8B%A9%E5%99%A8%EF%BC%9A%E5%AE%9A%E4%B9%89%E7%9A%84%E6%97%B6%E5%80%99%E7%94%A8%E9%80%97%E5%8F%B7%E9%9A%94%E5%BC%80"><span class="toc-text">3、并集选择器：定义的时候用逗号隔开</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E4%B8%80%E4%BA%9B-CSS3-%E9%80%89%E6%8B%A9%E5%99%A8"><span class="toc-text">一些 CSS3 选择器</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%B5%8F%E8%A7%88%E5%99%A8%E7%9A%84%E5%85%BC%E5%AE%B9%E6%80%A7%E9%97%AE%E9%A2%98"><span class="toc-text">浏览器的兼容性问题</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#1-%E5%AD%90%E4%BB%A3%E9%80%89%E6%8B%A9%E5%99%A8%EF%BC%8C%E7%94%A8%E7%AC%A6%E5%8F%B7-gt-%E8%A1%A8%E7%A4%BA"><span class="toc-text">1.子代选择器，用符号&gt;表示</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#2-%E5%BA%8F%E9%80%89%E6%8B%A9%E5%99%A8"><span class="toc-text">2.序选择器</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#3-%E4%B8%8B%E4%B8%80%E4%B8%AA%E5%85%84%E5%BC%9F%E9%80%89%E6%8B%A9%E5%99%A8"><span class="toc-text">3.下一个兄弟选择器</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E6%88%91%E7%9A%84%E5%85%AC%E4%BC%97%E5%8F%B7"><span class="toc-text">我的公众号</span></a></li></ol>
</div>
            </main>
            <aside class="right-column">
              <div class="sticky-widescreen">
  
  
    <article class="card card-content toc-card">
      <div class="toc-header">
  <i 
    class="iconfont icon-menu" 
    style="padding-right: 2px;">
  </i>目录
</div>
<ol class="toc"><li class="toc-item toc-level-2"><a class="toc-link" href="#%E6%9C%AC%E6%96%87%E4%B8%BB%E8%A6%81%E5%86%85%E5%AE%B9"><span class="toc-text">本文主要内容</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%89%8D%E8%A8%80"><span class="toc-text">前言</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#CSS-%E6%A6%82%E8%BF%B0"><span class="toc-text">CSS 概述</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#CSS-%E7%9A%84%E9%87%8D%E7%82%B9%E7%9F%A5%E8%AF%86%E7%82%B9"><span class="toc-text">CSS 的重点知识点</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#CSS-%E6%95%B4%E4%BD%93%E6%84%9F%E7%9F%A5"><span class="toc-text">CSS 整体感知</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#CSS-%E8%AF%AD%E6%B3%95"><span class="toc-text">CSS 语法</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#css-%E4%BB%A3%E7%A0%81%E7%9A%84%E6%B3%A8%E9%87%8A"><span class="toc-text">css 代码的注释</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#CSS-%E7%9A%84%E4%B8%80%E4%BA%9B%E7%AE%80%E5%8D%95%E5%B8%B8%E8%A7%81%E7%9A%84%E5%B1%9E%E6%80%A7"><span class="toc-text">CSS 的一些简单常见的属性</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#CSS-%E7%9A%84%E4%B9%A6%E5%86%99%E6%96%B9%E5%BC%8F"><span class="toc-text">CSS 的书写方式</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#1%E3%80%81CSS-%E5%92%8C-HTML-%E7%BB%93%E5%90%88%E6%96%B9%E5%BC%8F%E4%B8%80%EF%BC%9A%E8%A1%8C%E5%86%85%E6%A0%B7%E5%BC%8F"><span class="toc-text">1、CSS 和 HTML 结合方式一：行内样式</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#2%E3%80%81CSS-%E5%92%8C-HTML-%E7%BB%93%E5%90%88%E6%96%B9%E5%BC%8F%E4%BA%8C%EF%BC%9A%E5%86%85%E5%B5%8C%E6%A0%B7%E5%BC%8F%E8%A1%A8"><span class="toc-text">2、CSS 和 HTML 结合方式二：内嵌样式表</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#3%E3%80%81CSS-%E5%92%8C-HTML-%E7%BB%93%E5%90%88%E6%96%B9%E5%BC%8F%E4%B8%89%EF%BC%9A%E5%BC%95%E5%85%A5%E5%A4%96%E9%83%A8%E6%A0%B7%E5%BC%8F%E8%A1%A8-css-%E6%96%87%E4%BB%B6"><span class="toc-text">3、CSS 和 HTML 结合方式三：引入外部样式表 css 文件</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#CSS-%E7%9A%84%E5%9B%9B%E7%A7%8D%E5%9F%BA%E6%9C%AC%E9%80%89%E6%8B%A9%E5%99%A8"><span class="toc-text">CSS 的四种基本选择器</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#1%E3%80%81%E6%A0%87%E7%AD%BE%E9%80%89%E6%8B%A9%E5%99%A8%EF%BC%9A%E9%80%89%E6%8B%A9%E5%99%A8%E7%9A%84%E5%90%8D%E5%AD%97%E4%BB%A3%E8%A1%A8-html-%E9%A1%B5%E9%9D%A2%E4%B8%8A%E7%9A%84%E6%A0%87%E7%AD%BE"><span class="toc-text">1、标签选择器：选择器的名字代表 html 页面上的标签</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#2%E3%80%81ID-%E9%80%89%E6%8B%A9%E5%99%A8%EF%BC%9A%E8%A7%84%E5%AE%9A%E7%94%A8-%E6%9D%A5%E5%AE%9A%E4%B9%89"><span class="toc-text">2、ID 选择器：规定用#来定义</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#3%E3%80%81%E7%B1%BB%E9%80%89%E6%8B%A9%E5%99%A8%EF%BC%9A%E8%A7%84%E5%AE%9A%E7%94%A8%E5%9C%86%E7%82%B9-%E6%9D%A5%E5%AE%9A%E4%B9%89"><span class="toc-text">3、类选择器：规定用圆点.来定义</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#4%E3%80%81%E9%80%9A%E9%85%8D%E7%AC%A6-%EF%BC%9A%E5%8C%B9%E9%85%8D%E4%BB%BB%E4%BD%95%E6%A0%87%E7%AD%BE"><span class="toc-text">4、通配符*：匹配任何标签</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#CSS-%E7%9A%84%E5%87%A0%E7%A7%8D%E9%AB%98%E7%BA%A7%E9%80%89%E6%8B%A9%E5%99%A8"><span class="toc-text">CSS 的几种高级选择器</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#1%E3%80%81%E5%90%8E%E4%BB%A3%E9%80%89%E6%8B%A9%E5%99%A8-%E5%AE%9A%E4%B9%89%E7%9A%84%E6%97%B6%E5%80%99%E7%94%A8%E7%A9%BA%E6%A0%BC%E9%9A%94%E5%BC%80"><span class="toc-text">1、后代选择器: 定义的时候用空格隔开</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#2%E3%80%81%E4%BA%A4%E9%9B%86%E9%80%89%E6%8B%A9%E5%99%A8%EF%BC%9A%E5%AE%9A%E4%B9%89%E7%9A%84%E6%97%B6%E5%80%99%E7%B4%A7%E5%AF%86%E7%9B%B8%E8%BF%9E"><span class="toc-text">2、交集选择器：定义的时候紧密相连</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#3%E3%80%81%E5%B9%B6%E9%9B%86%E9%80%89%E6%8B%A9%E5%99%A8%EF%BC%9A%E5%AE%9A%E4%B9%89%E7%9A%84%E6%97%B6%E5%80%99%E7%94%A8%E9%80%97%E5%8F%B7%E9%9A%94%E5%BC%80"><span class="toc-text">3、并集选择器：定义的时候用逗号隔开</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E4%B8%80%E4%BA%9B-CSS3-%E9%80%89%E6%8B%A9%E5%99%A8"><span class="toc-text">一些 CSS3 选择器</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%B5%8F%E8%A7%88%E5%99%A8%E7%9A%84%E5%85%BC%E5%AE%B9%E6%80%A7%E9%97%AE%E9%A2%98"><span class="toc-text">浏览器的兼容性问题</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#1-%E5%AD%90%E4%BB%A3%E9%80%89%E6%8B%A9%E5%99%A8%EF%BC%8C%E7%94%A8%E7%AC%A6%E5%8F%B7-gt-%E8%A1%A8%E7%A4%BA"><span class="toc-text">1.子代选择器，用符号&gt;表示</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#2-%E5%BA%8F%E9%80%89%E6%8B%A9%E5%99%A8"><span class="toc-text">2.序选择器</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#3-%E4%B8%8B%E4%B8%80%E4%B8%AA%E5%85%84%E5%BC%9F%E9%80%89%E6%8B%A9%E5%99%A8"><span class="toc-text">3.下一个兄弟选择器</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E6%88%91%E7%9A%84%E5%85%AC%E4%BC%97%E5%8F%B7"><span class="toc-text">我的公众号</span></a></li></ol>
    </article>
  
  
  <article class="card card-content">
    <div class="recent-posts-card">
  <div class="recent-posts-header">
    <i 
      class="iconfont icon-wenzhang_huaban" 
      style="padding-right: 2px;">
    </i>最近文章
  </div>
  <div class="recent-posts-list">
    
      <div class="recent-posts-item">
        <div class="recent-posts-item-title">2023-03-09</div>
        <a href="/2023/03/09/13-React%E5%9F%BA%E7%A1%80/11-React%20Navive%E5%88%9D%E8%AF%86/"><div class="recent-posts-item-content">11-React Navive初识</div></a>
      </div>
    
      <div class="recent-posts-item">
        <div class="recent-posts-item-title">2023-03-09</div>
        <a href="/2023/03/09/13-React%E5%9F%BA%E7%A1%80/10-AntD%E6%A1%86%E6%9E%B6%E7%9A%84upload%E7%BB%84%E4%BB%B6%E4%B8%8A%E4%BC%A0%E5%9B%BE%E7%89%87%E6%97%B6%E4%BD%BF%E7%94%A8customRequest%E6%96%B9%E6%B3%95%E8%87%AA%E5%AE%9A%E4%B9%89%E4%B8%8A%E4%BC%A0%E8%A1%8C%E4%B8%BA/"><div class="recent-posts-item-content">10-AntD框架的upload组件上传图片时使用customRequest方法自定义上传行为</div></a>
      </div>
    
      <div class="recent-posts-item">
        <div class="recent-posts-item-title">2023-03-09</div>
        <a href="/2023/03/09/13-React%E5%9F%BA%E7%A1%80/09-AntD%E6%A1%86%E6%9E%B6%E7%9A%84upload%E7%BB%84%E4%BB%B6%E4%B8%8A%E4%BC%A0%E5%9B%BE%E7%89%87%E6%97%B6%E9%81%87%E5%88%B0%E7%9A%84%E4%B8%80%E4%BA%9B%E5%9D%91/"><div class="recent-posts-item-content">09-AntD框架的upload组件上传图片时遇到的一些坑</div></a>
      </div>
    
      <div class="recent-posts-item">
        <div class="recent-posts-item-title">2023-03-09</div>
        <a href="/2023/03/09/13-React%E5%9F%BA%E7%A1%80/08-Ant%20Design%E7%9A%84%E5%9F%BA%E6%9C%AC%E4%BD%BF%E7%94%A8/"><div class="recent-posts-item-content">08-Ant Design的基本使用</div></a>
      </div>
    
  </div>
</div>
  </article>
  
  
</div>
            </aside>
          </div>
        </div>
      </div>
    </div>
     
    <footer class="footer">
  <div class="footer-container">
    <div>
      <div class="footer-dsc">
        <span>
          Copyright ©
          
            2023
          
          
        </span>
        &nbsp;
        <a 
          href="/" 
          class="footer-link">
          前端面试
        </a>
      </div>
    </div>

    
      <div class="footer-dsc">
        
          Powered by
          <a 
            href="https://hexo.io/" 
            class="footer-link" 
            target="_blank" 
            rel="nofollow noopener noreferrer">
            &nbsp;Hexo
          </a>
        
        
          <span>&nbsp;|&nbsp;</span>
        
        
          Theme -
          <a 
            href="https://github.com/theme-kaze" 
            class="footer-link" 
            target="_blank"
            rel="nofollow noopener noreferrer">
            &nbsp;Kaze
          </a>
        
      </div>
    
    
    
    
</footer>
 
    
  <a 
    role="button" 
    id="scrollbutton" 
    class="basebutton" 
    aria-label="回到顶部">
    <i class="iconfont icon-arrowleft button-icon"></i>
  </a>

<a 
  role="button" 
  id="menubutton"
  aria-label="menu button"
  class="basebutton">
  <i class="iconfont icon-menu button-icon"></i>
</a>
<a 
  role="button" 
  id="popbutton" 
  class="basebutton" 
  aria-label="控制中心">
  <i class="iconfont icon-expand button-icon"></i>
</a>
<a 
  role="button" 
  id="darkbutton" 
  class="basebutton darkwidget" 
  aria-label="夜色模式">
  <i class="iconfont icon-weather button-icon"></i>
</a>
<a 
  role="button" 
  id="searchbutton" 
  class="basebutton searchwidget" 
  aria-label="搜索">
  <i class="iconfont icon-search button-icon"></i>
</a> 
     
     
     
      <script>
  var addImgLayout = function () {
    var img = document.querySelectorAll('.post-content img')
    var i
    for (i = 0; i < img.length; i++) {
      var wrapper = document.createElement('a')
      wrapper.setAttribute('href', img[i].getAttribute('data-src'))
      wrapper.setAttribute('aria-label', 'illustration')
      wrapper.style.cssText =
        'width: 100%; display: flex; justify-content: center;'
      if (img[i].alt) wrapper.dataset.caption = img[i].alt
      wrapper.dataset.nolink = true
      img[i].before(wrapper)
      wrapper.append(img[i])
      var divWrap = document.createElement('div')
      divWrap.classList.add('gallery')
      wrapper.before(divWrap)
      divWrap.append(wrapper)
    }
    baguetteBox.run('.gallery')
  }
</script>
<script>
  loadScript(
    "/js/lib/lightbox/baguetteBox.min.js",
    addImgLayout
  )
</script>
 
     
     
    <script src="/js/main.js"></script> 
     
    
      <script>
        var addLazyload = function () {
          var observer = lozad('.lozad', {
            load: function (el) {
              el.srcset = el.getAttribute('data-src')
            },
            loaded: function (el) {
              el.classList.add('loaded')
            },
          })
          observer.observe()
        }
      </script>
      <script>
        loadScript('/js/lib/lozad.min.js', addLazyload)
      </script>
    
    <script src="//instant.page/5.1.0" type="module"
      integrity="sha384-by67kQnR+pyfy8yWP4kPO12fHKRLHZPfEsiSXR8u2IKcTdxD805MGUXBzVPnkLHw"></script>
    
    
  </body>
</html>
